当前位置:首页 > Web开发 > 正文

wife){ var preareWife = "xiaozhang" ; this .name = name; th

2024-03-31 Web开发

标签:

1、传统形式 ----> 原型链
过多的担任了没用的属性

Grand.prototype.lastName = "1"; function Grand(){ } var grand = new Grand(); Father.prototype = grand; function Father() { } var father = new Father(); Son.prototype = father; function Son() { } var son = new Son();

2、借用结构函数
不能担任借用结构函数的原型
每次结构函数都要夺多走一个函数

function Person(name, sex, age) { this.name = name; this.sex = sex; this.age = age; } function Student(name, sex, age, grand) { Person.call(this, name, sex, age); this.grand = grand; } var student = new Student();

3、共享原型 //错误谬误 :一个值改另一个也跟这改
不能随便窜改本身的原型

Father.prototype.lastName = ‘Deng‘; function Father() { } function Son() { } function inherit(Target , Origin){ Target.prototype = Origin.prototype } inherit(Son , Father); var son = new Son(); var father = new Father();

4、圣杯模式

function inherit(Target , Origin){ function F(){} F.prototype = Origin.prototype; Target.prototype = new F(); Target.prototype.constructor = Target; Target.prototype.uber = Origin.prototype; } Father.prototype.lastName = ‘Deng‘; function Father() { } function Son() { } inherit(Son , Father); var son = new Son(); var father = new Father();

私有化变量的应用

function Li(name , wife){ var preareWife = "xiaozhang"; this.name = name; this.wife = wife; this.divorce = function () { this.wife = preareWife; }; this.changPrepareWife = function (target) { preareWife = target; }; this.sayPrapreWife = function () { console.log(preareWife); } } var li = new Li("li" , "xiaoLi")

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31274.html