zoukankan      html  css  js  c++  java
  • JavaScript 学习笔记

    本文是JavaScript The Good Part 有关对象和继承的学习笔记。

    1. Object.create

    本函数是ECMAScript 5中的标准函数,其作用是用一个对象作为原型来生成另一个对象,可以用以下的code 模拟实现。

        if(typeof Object.create !== 'function') {
            Object.create = function(proto){
                var F = function(){};
                if(typeof proto !== 'object'){
                    //Follow the chrome error pattern.
                    throw new TypeError('Object prototype may only be an Object or null: ' + proto);
                }
                F.prototype = proto;
                return new F();
            };
        }

      

    具体的想法就是利用一个内部的函数作为委托,将所需的新对象和原来的对象隔离开来。

    关于出错处理是根据Chrome现在的错误信息生成的。

    2. new

    new 是JavaScript里面的一个关键字,其具体的作用,请参考 MDN。 可以用以下的Code进行模拟。

        _new = function(constructor, args){
            var that = Object.create(constructor.prototype), 
                other = constructor.apply(that, args);
            return (typeof(other) === 'object' && other) || that;
        }

     3. 继承

    3.1 伪传统方式 (Pseudoclassical)

    书中并没有推荐这种模拟传统继承的方式。因为它偏离了JavaScript原型继承的特性。

    3.2 原型(Prototypal)

    其示例代码如下所示。

    var myMammal = {
        name : 'Herb the Mammal',
        get_name : function ( ) {
        return this.name;
        },
        says : function ( ) {
        return this.saying || '';
        }
    };
    
    var myCat = Object.create(myMammal);
    myCat.name = 'Henrietta';
    myCat.saying = 'meow';
    myCat.purr = function (n) {
        var i, s = '';
        for (i = 0; i < n; i += 1) {
            if (s) {
                s += '-';
            }
            s += 'r';
        }
        return s;
    };
    myCat.get_name = function ( ) {
        return this.says( ) + ' ' + this.name + ' ' + this.says( );
    };

    子类不会拥有父类构造函数中的属性和方法,不过没有对类型中的私有变量进行保护。

    3.3 函数(Functional)

    var constructor = function (spec, my) {
      var that, other private instance variables;
      my = my || {};
      Add shared variables and functions to my
      that = a new object;
      Add privileged methods to that
      return that;
    };

    加粗的地方需要替换成实际的代码。

    需要说明的地方如下。

    • spec是构造对象所需的信息。
    • my是构造函数可以分享给其他对象的信息,不是必需的。
    • 构造一个对象(a new object)可以是字面量,使用new构造,使用Object.create,调用其他返回对象的函数。
    • 函数内部的私有属性是外部不能看到的。
    • 添加私有函数的方法如下所示。
    var methodical = function ( ) {
        ...
    };
    that.methodical = methodical;

    例如我们在调用methodical的时候,就直接使用内部的methodical,而不使用that.methodical,这样可以防止此函数被篡改掉。

    书中的例子如下。

    vvar mammal = function (spec) {
        var that = {};
        that.get_name = function ( ) {
            return spec.name;
        };
        that.says = function ( ) {
            return spec.saying || '';
        };
        return that;
    };
    var myMammal = mammal({name: 'Herb'});
        var cat = function (spec) {
        spec.saying = spec.saying || 'meow';
        var that = mammal(spec);
        that.purr = function (n) {
            var i, s = '';
            for (i = 0; i < n; i += 1) {
                if (s) {
                    s += '-';
                }
                s += 'r';
            }
            return s;
        };
        that.get_name = function ( ) {
            return that.says( ) + ' ' + spec.name + ' ' + that.says( );
        }
        return that;
    };
    var myCat = cat({name: 'Henrietta'});
  • 相关阅读:
    UDP:用户数据报协议(User Datagram Protocol)
    线程池的使用
    SQL Server表和模式空间使用情况http://www.51myit.com/thread2466911.html
    bytetobmp and bmptobyte(Image)
    c# TCP例子转载
    POJ 4047Garden
    NYOJ 102 次方求模
    Sum
    POJ 1094 Sorting It All Out(经典拓扑,唯一排序)
    POJ 2387 Til the Cows Come Home(Dijkstra)
  • 原文地址:https://www.cnblogs.com/rixin/p/4066351.html
Copyright © 2011-2022 走看看