1、prototype的定义
javascript
中的每个对象都有prototype
属性,Javascript
中对象的prototype
属性的解释是:返回对象类型原型的引用。
每一个构造函数都有一个属性叫做原型。这个属性非常有用:为一个特定类声明通用的变量或者函数。
你不需要显式地声明一个prototype
属性,因为在每一个构造函数中都有它的存在。你可以看看下面的例子:
function Test(){}
console.log(Test.prototype); // 输出 {constructor: ƒ}
1.1、 原型法设计模式
原型法的主要思想是,现在有1个类A
,我想要创建一个类B
,这个类是以A
为原型的,并且能进行扩展。我们称B的原型为A
。
1.2、javascript
的方法可以分为三类:
-
类方法
-
对象方法:放在实例对象上的方法
-
原型方法:放在原型上的方法
function People(name) {
this.name = name;
//对象方法
this.Introduce = function () {
console.log("My name is " + this.name);
}
}
//类方法
People.Run = function () {
console.log("I can run");
}
//原型方法
People.prototype.IntroduceChinese = function () {
console.log("我的名字是" + this.name);
}
//测试
var p1 = new People("Windking");
p1.Introduce();
People.Run();
p1.IntroduceChinese();
1.3、给prototype
添加属性
就如你在上面所看到的,prototype
是一个对象,因此,你能够给它添加属性。你添加给prototype
的属性将会成为使用这个构造函数创建的对象的通用属性。
例如,我下面有一个数据类型Fish
,我想让所有的鱼都有这些属性:livesIn="water"和price=20;
为了实现这个,我可以给构造函数Fish
的prototype
添加那些属性。
function Fish(name, color) {
this.name = name;
this.color = color;
}
Fish.prototype.livesIn = "water";
Fish.prototype.price = 20;
var fish1 = new Fish("mackarel", "gray");
var fish2 = new Fish("goldfish", "orange");
var fish3 = new Fish("salmon", "white");
console.log(fish1.name + "," + fish1.color + "," + fish1.livesIn + "," + fish1.price);
console.log(fish2.name + "," + fish2.color + "," + fish2.livesIn + "," + fish2.price);
console.log(fish3.name + "," + fish3.color + "," + fish3.livesIn + "," + fish3.price);
输出是:
“mackarel, gray, water, 20”
“goldfish, orange, water, 20”
“salmon, white water, 20”
你看到所有的鱼都有属性livesIn
和price
,我们甚至都没有为每一条不同的鱼特别声明这些属性。这时因为当一个对象被创建时,这个构造函数 将会把它的属性prototype
赋给新对象的内部属性__proto__
。这个__proto__
被这个对象用来查找它的属性。
你也可以通过prototype
来给所有对象添加 共用的函数。这有一个好处:你不需要每次在构造一个对象的时候创建并初始化这个函数。
1.4、用prototype
给对象添加函数
function Employee(name, salary){
this.name=name;
this.salary=salary;
}
Employee.prototype.getSalary=function getSalaryFunction(){
return this.salary;
}
Employee.prototype.addSalary=function addSalaryFunction(addition){
this.salary=this.salary+addition;
}
var boss1=new Employee("Joan", 200000);
var boss2=new Employee("Kim", 100000);
var boss3=new Employee("Sam", 150000);
输出的结果为:
console.log(boss1.getSalary()); // 输出 200000
console.log(boss2.getSalary()); // 输出 100000
console.log(boss3.getSalary()); // 输出 150000
子类如何重写父类的属性或方法:
function AClass() {
this.Property = 1;
this.Method = function () {
console.log(1);
}
}
function AClass2() {
this.Property2 = 2;
this.Method2 = function () {
console.log(2);
}
}
AClass2.prototype = new AClass();
AClass2.prototype.Property = 3;
AClass2.prototype.Method = function () {
console.log(4);
}
var obj = new AClass2();
console.log(obj.Property);
obj.Method();
输出结果为:
3
4
可以在对象上增加属性或方法
function Aclass() {
this.Property = 1;
this.Method = function () {
console.log(1);
}
}
var obj = new Aclass();
obj.Property2 = 2;
obj.Method2 = function () {
console.log(2);
}
console.log(obj.Property2);
obj.Method2();
输出结果为:
2
2
在外部不能通过prototype
改变自定义类型的属性或方法。该例子可以看到:调用的属性和方法仍是最初定义的结果。即当原型方法和对象方法在调用相同的属性和函数时,会执行对象方法里面的属性和函数。
function Aclass() {
this.Property = 1;
this.Method = function () {
console.log(1);
}
}
Aclass.prototype.Property = 2;
Aclass.prototype.Method = function () {
console.log(2);
}
var obj = new Aclass();
console.log(obj.Property);
obj.Method();
输出结果为:
1
1
1.5、A.prototype = new B();
理解prototype
不应把它和继承混淆。A
的prototype
为B
的一个实例,可以理解A
将B
中的方法和属性全部克隆了一遍。A
能使用B
的方法和属性。这里强调的是克隆而不是继承。可以出现这种情况:A
的prototype
是B
的实例,同时B
的prototype
也是A
的实例。
先看一个实验的例子:
function baseClass(){
this.showMsg = function(){
console.log("baseClass::showMsg");
}
}
function extendClass(){}
extendClass.prototype = new baseClass();
var instance = new extendClass();
instance.showMsg();
输出结果:
baseClass::showMsg
我们首先定义了baseClass
类,然后我们要定义extentClass
,但是我们打算以baseClass
的一个实例为原型,来克隆的extendClass
也同时包含showMsg
这个对象方法。
extendClass.prototype = new baseClass()
就可以阅读为:extendClass
是以baseClass
的一个实例为原型克隆创建的。
那么就会有一个问题,如果extendClass
中本身包含有一个与baseClass
的方法同名的方法会怎么样?
下面是扩展实验2:
function baseClass() {
this.showMsg = function () {
console.log("baseClass::showMsg");
}
}
function extendClass() {
this.showMsg = function () {
console.log("extendClass::showMsg");
}
}
extendClass.prototype = new baseClass();
var instance = new extendClass();
instance.showMsg();
输出结果:
extendClass::showMsg
实验证明:函数运行时会先去本体的函数中去找,如果找到则运行,找不到则去prototype
中寻找函数。或者可以理解为prototype
不会克隆同名函数。
那么又会有一个新的问题:如果我想使用extendClass
的一个实例instance
调用baseClass
的对象方法showMsg
怎么办?
答案是可以使用call:
extendClass.prototype = new baseClass();
var instance = new extendClass();
var baseinstance = new baseClass();
baseinstance.showMsg.call(instance);//显示baseClass::showMsg
输出:
baseClass::showMsg
这里的baseinstance.showMsg.call(instance)
;阅读为就是把baseinstance
的showMsg
放在instance
执行
或者理解为:将instance
当做baseinstance
来调用,调用它的对象方法showMsg
好了,这里可能有人会问,为什么不用baseClass.showMsg.call(instance);
这就是对象方法和类方法的区别,我们想调用的是baseClass
的对象方法
1.6、对象方法与通过new创建对象的重要区别
这个区别就是function
定义的方法(对象方法)有一个prototype
属性,使用new
生成的对象就没有这个prototype
属性。也就是prototype
属性是对象方法或者构造方法的专有属性。 prototype
属性又指向了一个prototype
对象,注意prototype
属性与prototype
对象是两个不同的东西,要注意区别。在prototype
对象中又有一个constructor
属性,这个constructor
属性同样指向一个constructor对象,而这个constructor
对象恰恰就是这个function
函数本身。如下所示:
function Person(name)
{
this.name=name;
this.showMe=function()
{
console.log(this.name);
}
};
var one=new Person('js');
console.log(one.prototype)//undefined
console.log(typeof Person.prototype);//object
console.log(Person.prototype.constructor);//function Person(name) {...};
输出
undefined
object
function Person(name) {…};
最后,下面这个代码如果理解清晰,那么这篇文章说的就已经理解了:
function baseClass() {
this.showMsg = function () {
console.log("baseClass::showMsg");
}
this.baseShowMsg = function () {
console.log("baseClass::baseShowMsg");
}
}
baseClass.showMsg = function () {
console.log("baseClass::showMsg static");
}
function extendClass() {
this.showMsg = function () {
console.log("extendClass::showMsg");
}
}
extendClass.showMsg = function () {
console.log("extendClass::showMsg static")
}
extendClass.prototype = new baseClass();
var instance = new extendClass();
instance.showMsg(); //显示extendClass::showMsg
instance.baseShowMsg(); //显示baseClass::baseShowMsg
instance.showMsg(); //显示extendClass::showMsg
baseClass.showMsg.call(instance); //显示baseClass::showMsg static
var baseinstance = new baseClass();
baseinstance.showMsg.call(instance); //显示baseClass::showMsg