zoukankan      html  css  js  c++  java
  • 理解原型

    function Box(){
    this.aaa=123;
    }
    var box=new Box();

    console.log(Box);
    console.log(box);
    console.log(box.aaa);
    console.log(Box.prototype);
    console.log(box.constructor);
    console.log(box.__proto__);
    console.log(box.constructor==Box);
    console.log(Box.prototype==box.__proto__);
    console.log(box.__proto__.constructor.__proto__.constructor);
    console.log(box.__proto__.constructor);

    结果:

    来自群里的问题

    function a () {
      this.c= function () {
        console.log('c');
      }
    };
    a.b = function () {
       console.log('b');
    };
    var e = new a ();
    console.log(e._________)  //问题:a的实例得到b方法 不用call,apply

    答案:

    //首先new以后 e.__proto__=a.prototype,当想调用e的b方法时,首先e没有b方法和属性,于是他要到他的__proto__中去找,也就是a.prototype中,但又没在他的原型里写,所以就找不到
    console.log(e.__proto__.constructor.b());

    来自jquery的理解

    function pub(){
        return new pub.prototype.init();
    }
    pub.prototype.init=function(){
        this.css();
    }
    pub.prototype.css=function(){
        console.log("123");
    }
    pub.prototype.init.prototype=pub.prototype;
    pub().css();
    //pub.prototype.init.prototype=pub.prototype;形成了对象互相引用,所以才能用pub().css()

  • 相关阅读:
    POJ Problem 1363 Rails 【栈】
    POJ Problem 3040 Allowance 【贪心】
    ACM 程序对拍
    HDU Problem
    POJ
    HDU Problem
    HDU Problem—2124 Repair the Wall 【贪心】
    HDU Problem 1052 Tian Ji -- The Horse Racing 【贪心】
    POJ Problem Radar Installation 【贪心】
    Beyond Compare和输出文件比较的方法
  • 原文地址:https://www.cnblogs.com/change-oneself/p/5036018.html
Copyright © 2011-2022 走看看