构造函数数组
基本概念浏览器
继承方式安全
<script> function Person(name, age) { this.name = name; this.age = age; } //不只拥有构造函数中的属性,还拥有原型中建立出来的属性 Person.prototype.gender = 'male'; var p = new Person('qx', 18); console.log(p.name);//qx console.log(p.age);//18 console.log(p.gender);//male </script>
<script> var o = {} var obj = { name: "张三", age: 18, sayHello: function () { console.log("Hello world"); } } //混入式继承 for (var k in obj) { o[k] = obj[k]; } console.log(o); </script>
一、最先的原理函数
<script> //经过替换原型,使得被建立出来对象也拥有传入对象的属性 function jicheng(obj) { var o = {}; o.__proto__ = obj; return o; } var o = jicheng({name: "张三"}); console.log(o); </script>
二、create方法this
<script> var o = { name: "周三" }; var obj = Object.create(o); console.log(obj.name); </script>
三、create方法存在兼容性问题spa
<script> var obj = { name:"周三" }; //检测浏览器的能力,若是没有Object.create方法就给他添加一个(不推荐使用) if(Object.create){ var o = Object.create(obj); }else{ Object.create = function () { function F() { } F.prototype = obj; var o = new F(); } var o = Object.create(obj); } </script>
<script> //本身定义个函数 function create(obj) { if (Object.create) { return Object.create(obj); } else { function F() { } F.prototype = obj; return new F(); } } </script>
原型对象prototype
<script> function Person(name, age) { this.name = name; this.age = age; } Person.prototype.say=function () { console.log('chi'); } var p=new Person('wx',18); p.say();//chi </script>
<script> function Person(name, age) { this.name = name; this.age = age; } Person.prototype={ say:function () { console.log('chi'); }, play:function () { console.log('wan'); } }; var p=new Person('wx',18); p.say();//chi p.play();//wan </script>
<script> function Person(name, age) { this.name = name; this.age = age; } console.dir(Person.prototype); Person.prototype = { say: function () { console.log('chi'); }, play: function () { console.log('wan'); } }; Person.prototype.constructor = Person; console.dir(Person.prototype); </script>
原型继承3d
将animal这个对象赋值给Person.prototype,与上面的经过字面量{}道理是同样的,毕竟animal也是一个对象,里面的属性也是被{}包裹的调试
<script> function Animal() { this.eat = 'chifan'; } var animal = new Animal(); function Person() { this.read = 'dushu'; } var p1 = new Person(); //person显示是具备animal的属性,毕竟人也是动物,具体方法是让person的原型被替换成animal //替换后,经过person构造函数建立出来的对象,不只具备person的属性,还具备animal的属性 Person.prototype = animal;//Person.prototype = new Animal()这样也行 var p2 = new Person(); console.log(p1); console.log(p2); </script>
输出结果:code
结果分析:
复杂原型继承
<script> function Yuanzi() { this.des = '进化'; } function Animal() { this.skill = '捕猎'; } function Human() { this.say = '说话'; } Animal.prototype = new Yuanzi(); Animal.prototype.constructor = Animal; Human.prototype = new Animal(); Human.prototype.constructor = Human; var h = new Human(); console.log(h); </script>
打印对象h:
构造器的做用
原型链基本概念
一、每一个构造函数都有原型对象
二、每一个对象都会有构造函数
三、每一个构造函数的原型都是一个对象
四、那么这个原型对象也会有构造函数
五、那么这个原型对象的构造函数也会有原型对象
六、这样就会造成一个链式的结构,称为原型链
七、经过修改原型链结构实现的继承,就叫作原型继承
八、三角关系到成立的必要性之一是 实例化对象p原型的原型与Object构造函数都是指向同一个对象(在内存中的内容与地址都是同样的)
consolo.log (p.__proto__.__proto__===Object.prototype)//true
属性搜索基本原则
原型对象的替换
上面负责原型继承的模式是进行的原型的替换,这样虽然方便了,可是也是有问题的
(1)给原型对象替换赋值,原型对象就会失去constructor属性
<script> function Person(name, age) { this.name = name; this.age = age; } var p = new Person('qx', 18); console.log(Person.prototype); Person.prototype = { say: function () { console.log(this.name); } }; console.log(Person.prototype); </script>
(2)固然即便替换了原型对象,可是实例化后的对象依然具备constructor属性,而这个属性属于对象的原型对象的原型对象,从上图能够看出,以及下图也看出,替换先后打印的结果是不同的。替换后,访问的是对象的原型对象的原型的构造函数
<script> function Person(name, age) { this.name = name; this.age = age; } var p = new Person('qx', 18); console.log(Person.prototype.constructor); Person.prototype = { say: function () { console.log(this.name); } }; console.log(Person.prototype.constructor); </script>
(3)对原型对象进行替换,其实能够看做是将一个对象赋值给了一个普通对象,相似var a={},咱们打印a,consolo.dir(a),结果是a并无构造函数的属性
(4)解决办法就是,替换了原型对象,必须在替换的对象上面加上constructor属性,而且赋值指向的是自身构造函数,和上面的复杂原型继承是同样的
<script> function Person() { } Person.prototype = { constructor: Person; } </script>
扩展内置对象
<script> function MyArray() { // this.name = '我是一个数组'; } MyArray.prototype = new Array(); var arr1 = new MyArray(); arr1.push(4) console.log(arr1); MyArray.prototype.say = function () { console.log('添加一个说的方法'); } var arr2=new MyArray(); arr2.say(); </script>