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_ 찾을 수 있습니다. (prototype 체인).
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 고해상도를 =;

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 같이 마운트 있지만 INIT 것이다 // 있지만 이때.
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 객체는 사람들이 프로토 타입의 사람으로부터 상속, 원형 사람들 속성이 될 속성!

우리는) (A 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