zoukankan      html  css  js  c++  java
  • js总结(三):面向对象,prototype ,oo模拟

    http://aralejs.org/class/docs/competitors.html

    http://javascript.crockford.com/prototypal.html

    prototype

    prototype属性的值实际就是Object对象的实例,js将忽视任何设置为原始值的prototype属性。

    protoype拥有Object()对象实例属性和方法,prototype 包含了2个属性,一个是constructor ,另外一个是__proto__。

    这个就是object的实例属性.

    原型链,实际返回的是链中找到的第一个结果。

    用新对象替换prototype属性会删除默认的constructor属性。new 创建的实例的constructor指向的是Object()了,而不是他的构造函数了。

    实例的constructor是靠prototype。


    var A =function(){ var b =1; } A.prototype = {}; var a = new A; console.log(a.constructor); console.log(a instanceof A);//true了

    function Object() { [native code] } 
    true

    var A =function(){
       var b =1;
    }
    A.prototype = {};
    var a = function(){};
    a.constructor = A;
    console.log(a instanceof A);//false 

    instanceof与constructor没有关系了

    Here is another formulation:

    Object.prototype.begetObject = function () {
        function F() {}
        F.prototype = this;
        return new F();
    };
    
    newObject = oldObject.begetObject();

    2007-04-02

    The problem with the object function is that it is global, and globals are clearly problematic. The problem with Object.prototype.begetObject is that it trips up incompetent programs, and it can produce unexpected results when begetObject is overridden.

    So I now prefer this formulation:

    if (typeof Object.create !== 'function') {
        Object.create = function (o) {
            function F() {}
            F.prototype = o;
            return new F();
        };
    }
    newObject = Object.create(oldObject);

    2008-04-07

    jquery对象如何构建  http://nuysoft.iteye.com/blog/1182087

    http://www.cnblogs.com/baochuan/archive/2012/11/22/2782343.html

  • 相关阅读:
    14.6 将运算分组为事务
    Android 取得 ListView中每个Item项目的值
    【编程题目】n 个骰子的点数
    【编程题目】扑克牌的顺子
    【编程题目】颠倒栈☆
    【编程题目】输出 1 到最大的 N 位数
    【编程题目】寻找丑数
    【编程题目】在字符串中删除特定的字符
    【编程题目】复杂链表的复制☆
    【编程题目】找出数组中两个只出现一次的数字 ★★(自己没做出来)
  • 原文地址:https://www.cnblogs.com/danghuijian/p/4009971.html
Copyright © 2011-2022 走看看