你根本不懂Javascript(3.5):原型继承

本文最初发布于http://szhshp.org/tech/2017/02/18/JavaSprite.html
转载请注明

Prototype

Nearly all objects in JavaScript are instances of Object; a typical object inherits properties (including methods) from Object.prototype.

javascript中的对象类似一个指向prototype的指针和一个自身的属性列表

  • prototype的修改将影响所有的对象
  • 每个function都有一个默认的属性prototype,而这个prototype的constructor默认指向这个函数

Prototype 继承

可以给prototype添加方法或者属性

//create a class
function Person(name) {
    this.name = name;
};
var p = new Person("Mike");
p.name;//Mike

//add a fun to prototype
Person.prototype.getName = function() { 
    return this.name; 
};
p.getName();//Mike

console.log(Person.prototype.constructor === Person); // true 
console.log(p.constructor === Person);  // true, 'p' is inherited from the Person.prototype 

同时注意,建立instance时是继承对应prototype,因此同名方法和属性会被重写

//create a class
function Person(name) {
    debugger; 
    this.name = name; // the attr 'name' was overwritten here
};
Person.prototype.name = "Bill";
var p = new Person("Mike");
p.name;//Mike, the attr 'name' was overwritten 

子类及原型继承

实现子类和原型继承的步骤:

  1. 继承prototype中定义的属性和方法;
  2. 继承构造函数中定义的属性和方法;
function Animal(name) {  
    this.name = name;  
}  
Animal.prototype.sex = "male";
Animal.prototype.info = function () {
        console.log("animal");  
}

// -------------------------------------------------------------
//Demo

var cat = new Animal('cat');


console.log(cat instanceof Animal);    //t
console.log(cat instanceof Object);    //t
console.log( typeof Animal.prototype);      //object  
console.log( typeof Animal.constructor);        //function  
console.log(Animal.prototype.constructor == Animal);    //true  

// -------------------------------------------------------------
// -------------------------------------------------------------

function People(name) {  
    this.name = name;  
}  
// prototype Is a obj, here we create an instance and assign to People.prototype
// in this way, we can inherit the attr and func from parent class
People.prototype = new Animal("animal");  

// overwrite the func from parent class
People.prototype.info = function() {  
    console.log("people")  
};  

// -------------------------------------------------------------
//Demo2

var mike = new People("mike");  
console.log(mike.sex);//male  
mike.info();//People  


console.log(mike instanceof People);    //t
console.log(mike instanceof Animal);    //t
console.log(mike instanceof Object);    //t
console.log( typeof People.prototype);      //object, its an instance of Animal  
console.log( typeof People.constructor);        //function  
console.log(People.prototype.constructor == People);    //true  

JS的面向对象技术

封装

//create a class
function Person(name) {
    this.name = Person.prefix + name; 
    this.firstName = Person.getFirstName(); // this is a function
};

Person.prefix = "Mc";// private variables
Person.getFirstName = function(){ // can't access from instants
    return 'George';
}

var p = new Person("Mike");
p.name;//McMike
p.firstName();//George

Example

一个全面并且典型的纯OOP例子:

function Set() {          // This is the constructor
    this.values = {};     // The properties of this object hold the set
    this.n = 0;           // How many values are in the set
    this.add.apply(this, arguments);  // All arguments are values to add
}


// Add each of the arguments to the set.
Set.prototype.add = function () {
    for (var i = 0; i < arguments.length; i++) {  // For each argument
        var val = arguments[i];                  // The value to add to the set
        var str = Set._v2s(val);                 // Transform it to a string
        if (!this.values.hasOwnProperty(str)) {  // If not already in the set
            this.values[str] = val;              // Map string to value
            this.n++;                            // Increase set size
        }
    }
    return this;                                 // Support chained method calls
};


// Remove each of the arguments from the set.
Set.prototype.remove = function () {
    for (var i = 0; i < arguments.length; i++) {  // For each argument
        var str = Set._v2s(arguments[i]);        // Map to a string
        if (this.values.hasOwnProperty(str)) {   // If it is in the set
            delete this.values[str];             // Delete it
            this.n--;                            // Decrease set size
        }
    }
    return this;                                 // For method chaining
};


// Return true if the set contains value; false otherwise.
Set.prototype.contains = function (value) {
    return this.values.hasOwnProperty(Set._v2s(value));
};


// Return the size of the set.
Set.prototype.size = function () {
    return this.n;
};


// Call function f on the specified context for each element of the set.
Set.prototype.foreach = function (f, context) {
    for (var s in this.values)                 // For each string in the set
        if (this.values.hasOwnProperty(s))    // Ignore inherited properties
            f.call(context, this.values[s]);  // Call f on the value
};


Set._v2s = function (val) {         //这是一个内部函数,当然实例对象无法调用这个方法
    switch (val) {
        case undefined:
            return 'u';          // Special primitive
        case null:
            return 'n';          // values get single-letter
        case true:
            return 't';          // codes.
        case false:
            return 'f';
        default:
            switch (typeof val) {
                case 'number':
                    return '#' + val;    // Numbers get # prefix.
                case 'string':
                    return '"' + val;    // Strings get " prefix.
                default:
                    return '@' + objectId(val); // Objs and funcs get @
            }
    }


    // For any object, return a string. This function will return a different
    // string for different objects, and will always return the same string
    // if called multiple times for the same object. To do this it creates a
    // property on o. In ES5 the property would be nonenumerable and read-only.
    function objectId(o) {
        var prop = "|**objectid**|";   // Private property name for storing ids
        if (!o.hasOwnProperty(prop))   // If the object has no id
            o[prop] = Set._v2s.next++; // Assign it the next available
        return o[prop];                // Return the id
    }
};
Set._v2s.next = 100;    // Start assigning object ids at this value.

另一种通过返回值设定类的方法

function Test() {
    var map = 1;
    function a(){
        map = 2;
    }
    function b(){
        console.log(map);
    }
    return{
        a:a,
        b:b
    }
}
var t = new Test()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,736评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,167评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,442评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,902评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,302评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,573评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,847评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,562评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,260评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,531评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,021评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,367评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,016评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,068评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,827评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,610评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,514评论 2 269

推荐阅读更多精彩内容