zoukankan      html  css  js  c++  java
  • js原型链笔记

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>

    可正常计算的范围 小数点前 16位, 后16位。


    构造函数特点 大驼峰命名法 new


    原型:
    1.定义(prototype):
    原型是function对象的一个属性,它定义了构造函数制造出的对象的
    公共祖先。通过该构造函数产生的对象,可以继承改原型的属性和方
    法。原型也是对象。
    2.利用原型特点和概念,可以提取公有属性。
    3.对象如何查看原型-->隐式属性 __proto__ 里面指向的是原型(原型链的连接点)
    4.对象如何查看对象的构造函数-->constructor (手动可以更改)

    <!--a.sayName() sayName里面的this指向是,谁调用的这个方法,this结束指向谁 -->
    绝大多数对象的最终都会继承自Object.prototype Object.create(null)这个不会继承自Object.prototype
    Object.create(原型,例如 Person.prototype)

    call/apply:
    作用:改变this指向。
    区别:后面传的参数形式不同。
    差距:传参列表不同
    call:需要吧实参按照形参的个数传进去
    apply:需要传一个arguments


    继承发展史:
    1.传承形式-->原型链
    过多的继承了没用的属性
    2.借用构造函数(call/apply)
    不能继承借用构造函数的原型
    每次构造函数都要多走一个函数
    3.共享原型 (函数.prototype = 函数.prototype)
    不能随便改动自己的原型
    4.圣杯模式



    <script type="text/javascript">


    // Person.prototype 原型
    // Person.prototype = {} 是 var person = new Person(); 的祖先

    Person.prototype = {
    proto : 123,
    };
    function Person(name , age , sax) {
    this.name = name;
    this.age = age;
    this.sax = sax;
    }
    var person = new Person('旺飞', 20 , 'male');

    // 借用构造函数
    function Person(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
    }
    function Student(name, age, sex, tel, grade) {
    Person.call(this, name, age, sex);
    this.tel = tel;
    this.grade = grade;
    }
    var student = new Student('xie', 20, 'male', 111, 2019);



    // 继承
    // 1.圣杯模式
    Father.prototype.lastname = 'deng';
    function Father() {}
    function Son() {this.sex = 'hehe';}
    function inherit(Target, Origin) {
    function F() {}
    F.prototype = Origin.prototype;
    Target.prototype = new F();
    Target.prototype.constructor = Target;
    Target.prototype.uber = Origin.prototype; // 超类
    }
    inherit(Son, Father);
    var son = new Son();
    var father = new Father();

    //2. 圣杯模式TUI3 建议使用第二种

    Father.prototype.lastname = 'deng';
    function Father() {}
    function Son() {this.sex = 'hehe';}
    var inherit = (function () {
    var F = function () {};
    return function (Target, Origin) {
    F.prototype = Origin.prototype;
    Target.prototype = new F();
    Target.prototype.constructor = Target;
    Target.prototype.uber = Origin.prototype;
    }
    }());
    inherit(Son, Father);
    var son = new Son();
    var father = new Father();
    </script>


    </body>
    </html>
  • 相关阅读:
    MySQL5.7 容器化安装
    什么是架构?——软件系统架构的定义
    服务端高并发分布式架构演进之路(转)
    CentOS7 增加回环地址
    三句话看明白jdk收费吗
    【转载】C#里怎么把string类型转换成double
    【转载】如何查看自己网站的搜索引擎收录量和索引量
    【转载】c# datatable 判断值是否存在
    【转载】C#中Datatable修改列名
    【转载】C#使用typeof运算符获取对象变量的具体类型Type
  • 原文地址:https://www.cnblogs.com/xiewangfei123/p/12251893.html
Copyright © 2011-2022 走看看