パッケージのJSオブジェクト(II)

通常の方法でパッケージJSオブジェクト

1.従来のパッケージ

    function Person (name,age){ 
        this.name = name;
        this.age = age;
    }
    
    Pserson.prototype = {
       constructor:Person,
       say:function(){
           console.log('hello everyone!');
     }
    }

2.アップグレードバージョン(より一般的な)

    function Person (example){
        this._init_(example);
    }
    Pserson.prototype = {
        constructor : Person,
        _init_ : function(example) {
            this.name = example.name;
            this.age = example.age;
        }
        say:function(){
            console.log('hello everyone');
        }
    }

3.newの原則の実装

var myNew = function(constructor, args) {
    var obj = {};
    obj .__proto__ = constructor.prototype;
    var res = constructor.apply(obj , args);
    var type = typeof res;
    if (['string', 'number', 'boolean', 'null', 'undefined'].indexOf(type) !== -1) {
        return obj ;
    }
    return res;
}
説明:VAR OBJ = {}コンストラクト__proto__に空のオブジェクト属性割り当てプロトタイププロトタイププロトタイプコンストラクタオブジェクトOBJによって
、this.init(例)を行う。この文章を、被験体は、OBJとすることができますそのプロトタイプオブジェクトのメソッドを_init_下さい。(プロトタイプチェーン)。
var res = constructor.apply(obj,args);

objが配列としてながらパラメータ、関数呼び出しのコンテキストにあります。まあ、

this._init_(example);

objが実行されます

機能

_init_ : function(example) {
    this.name = example.name;
    this.age = example.age;
}

コンテキストOBJのために呼び出すには、oはまた、自身の名前、年齢性質を持つことになります。
コンストラクタで、オブジェクト、関数、および正規表現を含む複合型を返す場合、それはそうでない場合、戻りがobj、直接オブジェクトを返します。

VARタイプ= typeof演算のres;

if(['string','number','boolean','null','undefined'].indexOf(type) !== -1){
    return obj;
}
return res;

例えば

   function Person(name) {
        this.name = name;
    }
    Person.prototype.say = function() {
        console.log(this.name);
    }
    var jack = myFriend(Person, ['jack ']);
    console.log(jack );
    jack.say();

4.クラスパッケージのjQuery

jQueryオブジェクトが強い統合は関数として呼び出すことができている、関数呼び出し時間と呼ばれるオブジェクトとしても使用することができ、新しいインスタンスを返す必要がないかもしれません。

コード

    var Person = function(info){
     return new Person.prototype.init(info);
    }
    
    Person.prototype = {
        constructor: Person,
        init:function(){
            this.name = example.name.
        }
    }
    Person.prototype.init.prototype = Person.prototype;
  • このパッケージには、非常に賢い方法です。内部機能の操作対象の構造、彼は工場として機能している間。呼び出しプロトタイプを続行することはそう、直感的なアプローチではありません
    var Person = function(example){
            return new Person.fn.init(example);
        }
    
    Person.fn = Person.prototype = {
        constructor: Person,
        init:function(){
            this.name = info.name;
            this.say = function(){
                this.makeExp();
            }
        }
        makeExp:function(){
            console.log(this.name);
        }
    }

//従来の方法は、以下のmakeArray Person.prorotypeのようにマウントすることが、彼らは、このインスタンスを初期化します。
Person.fn.init.prototype = Person.fn;
最後に閉鎖でカプセル化

 var Person = (function(win) {
        var Person = function(name) {
            return new Person.fn.init(name);
        }

        Person.fn = Person.prototype = {
            constructor: Person,
            init: function(name) {
                this.name = name;
                this.say = function() {
                    this.makeExp();
                }
            },
            makeExp: function() {
                console.log(this.name);
            }
        }

        Person.fn.init.prototype = Person.fn;

        return Person;
    })()

例えば:

var people = Person('jack');
console.log(people);
people.say();

Object.create();
オブジェクトを構築する方法、オブジェクトは、人を渡す人を構築し、人々は人を継承することができます。

var Person = {
    name: 'jack',
    say: function() {
        console.log(this.name);
    }
}
var people = Object.create(Person);
console.log(people);
people.say();

Personオブジェクトはなっ原型は、人々は、プロトタイプの人から継承していること、人を属性属性!

私たちは)(Object.createを達成することができます

Object.create = function(prototype){
   function Fun(){};
   Fun.prototype = prototype;
   var obj = new Fun();
   return obj;
}

説明:
コンストラクタのプロトタイプとしての属性人、オブジェクトは、人へのプロトタイプオブジェクトとして構成することができます。

Object.create(プロトタイプ);プロトタイプインスタンスオブジェクトが継承作成

このアプローチに関する考察:
(1)質量参加のObject.prototype、プロトタイプのObject.prototypeの作成である場合

  • オブジェクトおよび新オブジェクト()同じObject.create(のObject.prototype)<==>新しい新規作成
    オブジェクト();

(2)パラメータが空またはnullを渡す場合、オブジェクトは、プロトタイプを作成していません、

  • これは、オブジェクトがエラーを出力しますのdocument.write()を使用していない原因
document.write()を印刷の原理をObject.prototype.toString()メソッドを呼び出しているので、
のdocument.write()が印刷されないように、オブジェクトは、方法がない、プロトタイプではありません

   由此延伸的知识点: 引用值都也是算作是对象,  所以都可以用document.write()打印; 原始值numebr, boolean,
   string都有自己对象的包装类, 借助此机制也是可以用document.write()打印出的;

  但undefined 和 null既不是引用值,也没有对应的包装类,    所以应该无法打印的,但大家会发现这两个值也是可是用document.write()打印的, 因为这两个值被设定为特殊值,   

document.write()、それは任意のメソッドを呼び出すことなくされた印刷が、ダイレクトプリントの値

おすすめ

転載: www.cnblogs.com/baimeishaoxia/p/11921384.html