饥人谷技术博客

JavaScript原型对象与继承

2017-09-14  本文已影响24人  _贺瑞丰

1.原型对象与构造函数

无论什么时候,创建了一个函数后。
1.函数拥有propertype属性。值为这个函数的原型对象的指针。
2.同时对于原型对象,其拥有constructor就指向原来的函数(什么值类型的)
3.原型对象开始只获得constructor属性(默认的)。
4.用构造函数构造对象,实例也会有prototype: 指向原型对象

function Person() {
}
//这里做了两件事。PS中指向了一个原型对象prototype.而原型对象也GET到了constructor属性和其他一堆属性。
 Person.prototype.name = '原型名'
 Person.prototype.age = '原型年纪'
 Person.prototype.job = '原型小白鼠'
 Person.prototype.sayname = function () {
    alert(this.name)
 }
console.log(Person.prototype.isPrototypeOf(person1)) //返回true
var person1 = new Person();
//这里新建了一个person1对象,并且构造函数作用域指向新对象,执行构造函数的代码,返回新对象。

1.1为了简洁而重写原型对象

function Person() {
}
 //原型对象用字面量形式创建,实际上constructor就变成了Object.
Person.prototype ={
  name : '原型名',
  age : '原型年纪',
  job : '原型小白鼠',
  sayname : function(){
    alert(this.name);
  }
} 
var person1 = new Person();

重写之后要注意,在重写之前新建的实例还是指向原来的原型对象的。

1.2原型对象的弊端

1.3构造与原型并用

还有几种构造函数模式未总结

2. 继承:原型链

基本思想是利用原型让一个引用类型继承另外一个引用类型的属性和方法。

2.1原型链细节

例子:

function 超集函数(){
  this.property = true;
  this.age = '1';
}
超集函数.prototype.getSuperValue = function(){
  return this.property;
};
超集函数.prototype.name = '我是超级原型函数对象'
function 子集函数(){
  this.subproperty = false;
}
//inherit from 超集函数
子集函数.prototype = new 超集函数();
子集函数.prototype.name = '我是子集原型'
子集函数.prototype.getSubValue = function (){
  return this.subproperty;
};
console.log(子集函数.prototype)
console.log((超集函数.prototype))
console.log(超集函数.prototype.isPrototypeOf(子集函数.prototype)) //true.

3.练习题

function foo(){
  foo.abc = function (){console.log('456')}
  this.def = this.abc;
  this.abc = function(){console.log('def')}
  abc = function(){console.log('@@@@@')}
  var abc = function () {console.log('$$$$$')}
}
foo.abc = function () {
  console.log('123')
}
foo.prototype = {
  abc : function (){console.log('abc')}
}
foo.abc();// 123
var f = new foo();
f.def();//abc
f.abc();//def
foo.abc()//456

上一篇下一篇

猜你喜欢

热点阅读