Person.prototype.name = "sunney" function Person() { } var person = new Person(); console.log(person.name); //"sunney Person.prototype.name = "cherry"; console.log(person.name); //"cherry Person.prototype = { name:"xiaowang" } console.log(person.name) //sunney 等同于 var obj = {name:"a"}; var obj1 = obj; obj = {name:"b"} console.log(obj1) //{name: "a"} console.log(obj) //{name: "b"} Person.prototype.name = "sunney" function Person() { } Person.prototype = { name:"xiaowang" } var person = new Person(); console.log(person.name); //xiaowang /** * this指向 * 谁调用就会指向谁 */ Person.prototype = { name:"a", sayName:function () { console.log(this.name) } } function Person() { this.name = "b" } var person = new Person(); console.log(person.sayName()) //b console.log(Person.prototype.sayName()) //a /** * call改变this指向 * 第一个参数代表指向,第二个开始代表实参 */ function Person(name,age) { this.name = name; this.age =age } var person = new Person("deng",300); var obj = {}; Person.call(obj,"cheng",100); console.log(obj); //{name: "cheng", age: 100} console.log(person);//Person {name: "deng", age: 300} /** * 工厂 */ function Wheel(wheelSize,style) { this.style = style; this.wheelSize = wheelSize; } function Sit(c,sitColor) { this.c = c; this.sitColor = sitColor; } function Model(height,width,len) { this.height = height; this.width = width; this.len = len; } function Car(wheelSize,style,c,sitColor,height,width,len) { Wheel.call(this,wheelSize,style) Sit.call(this,c,sitColor) Model.call(this,height,width,len) } var car = new Car(100,"花里胡哨","真皮","黄色",1900,4900,2000) //Car {style: "花里胡哨", wheelSize: 100, c: "真皮", sitColor: "黄色", height: 1900,4900,2000} /** * call 需要把实参按照形参列表的个数传进去(一一对应) * apply 需要传一个arguments//一定传数组 */ function Wheel(wheelSize,style) { this.style = style; this.wheelSize = wheelSize; } function Sit(c,sitColor) { this.c = c; this.sitColor = sitColor; } function Model(height,width,len) { this.height = height; this.width = width; this.len = len; } function Car(wheelSize,style,c,sitColor,height,width,len) { Wheel.apply(this,[wheelSize,style]) Sit.apply(this,[c,sitColor]) Model.apply(this,[height,width,len]) } var car = new Car(100,"花里胡哨","真皮","黄色",1900,4900,2000) //Car {style: "花里胡哨", wheelSize: 100, c: "真皮", sitColor: "黄色", height: 1900,4900,2000} /** * 继承同一个原型 */ Father.prototype.lastName = "Deng"; function Father() { } function Son() { } Son.prototype = Father.prototype; var son = new Son(); var father = new Father(); //都继承了 Father.prototype.lastName /** * 继承最终写法 */ function inner(B,A) { function F(){}; F.prototype = A.prototype; B.prototype = new F; B.prototype.constructor = B; //把constructor指向改成继承者自身 B.prototype.final = A.prototype; //继承自谁 保存下来 以后可能有用 } Father.prototype.name = "李"; function Father() {} function Son() {} inner(Son,Father); var son = new Son(); var father = new Father(); console.log(son.name) //李 console.log(father.name) //李 Son.prototype.age = "18"; console.log(son.age) //18 console.log(Father.prototype.age) //undefined var inner = (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; } }()) Father.prototype.name = "李"; function Father() {} function Son() {} inner(Son,Father); var son = new Son(); var father = new Father(); console.log(son.name) //李 console.log(father.name) //李 /** * 命名空间(运用闭包) */ var name = "bcd"; var init = (function(){ var name = "abc"; function callName() { console.log(name) } return function () { callName() } }()) init() //abc
原型链与call/apply
最新推荐文章于 2019-08-01 22:12:29 发布