欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Javascript中的Prototype到底是什么

 更新時(shí)間:2016年02月16日 16:52:24   作者:Darren Ji  
Javascript也是面向?qū)ο蟮恼Z(yǔ)言,但它是一種基于原型Prototype的語(yǔ)言,而不是基于類的語(yǔ)言。接下來(lái)通過(guò)本文給大家介紹Javascript中的Prototype到底是啥的相關(guān)知識(shí),感興趣的朋友參考下

Javascript也是面向?qū)ο蟮恼Z(yǔ)言,但它是一種基于原型Prototype的語(yǔ)言,而不是基于類的語(yǔ)言。在Javascript中,類和對(duì)象看起來(lái)沒(méi)有太多的區(qū)別。

什么是prototype:

function定義的對(duì)象有一個(gè)prototype屬性,prototype屬性又指向了一個(gè)prototype對(duì)象,注意prototype屬性與prototype對(duì)象是兩個(gè)不同的東西,要注意區(qū)別。在prototype對(duì)象中又有一個(gè)constructor屬性,這個(gè)constructor屬性同樣指向一個(gè)constructor對(duì)象,而這個(gè)constructor對(duì)象恰恰就是這個(gè)function函數(shù)本身。 是不是很繞?用偽代碼表示如下:

var function{
prototype:prototype{
constructor:constructor == function
}
}

還不明白?看圖吧:


prototype的作用:

這個(gè)prototype到底有什么作用呢?看下面的例子:

function jb51(){
}
jb51.prototype.name = "a";
var test = new jb51();
alert(test.name)//"a";

奇怪吧,明明沒(méi)有為test設(shè)置name屬性,可是為什么會(huì)有值?

這就是prototype的功勞了,uw3c中prototype屬性中的name對(duì)象,在uw3c被new構(gòu)造函數(shù)之后,被繼承到了對(duì)象test的屬性中。接著看:

var name = "js";
function jb51(name){
alert(this.name);//"css"
}
jb51.prototype.name = "css";
var test = new jb51();
test()

為什么alert的值不是“js”?這個(gè)過(guò)程大致如下:

var test={};
uw3c.call(test);

第一步是建立一個(gè)新對(duì)象(test)。

第二步將該對(duì)象(test)內(nèi)置的原型對(duì)象設(shè)置為構(gòu)造函數(shù)(就是uw3c)prototype 屬性引用的那個(gè)原型對(duì)象。

第三步就是將該對(duì)象(test)作為this 參數(shù)調(diào)用構(gòu)造函數(shù)(就是uw3c),完成成員設(shè)置等初始化工作。

其中第二步中出現(xiàn)了一個(gè)新名詞就是內(nèi)置的原型對(duì)象,注意這個(gè)新名詞跟prototype對(duì)象不是一回事, 為了區(qū)別我叫它inobj,inobj就指向了函數(shù)uw3c的prototype對(duì)象。在uw3c的prototype對(duì)象中出現(xiàn)的任何屬性或者函數(shù)都可以在test對(duì)象中直接使用,這個(gè)就是JS中的原型繼承了。

通常,這樣創(chuàng)建一個(gè)對(duì)象:

function person(name){
this.sayHi = function(){
alert('hi ' + this.name);
}
this.name = name;
}
var p = new person("dan");
p.sayHi(); 

以上,使用new關(guān)鍵字,通過(guò)對(duì)象(函數(shù)也是特殊對(duì)象)創(chuàng)建一個(gè)對(duì)象實(shí)例。

在基于類的語(yǔ)言中,屬性或字段通常都是在類中事先定義好了,但在Javascript中,在創(chuàng)建對(duì)象之后還可以為類添加字段。

function animal(){}
var cat = new animal();
cat.color = "green"; 

以上,color這個(gè)字段只屬于當(dāng)前的cat實(shí)例。
對(duì)于后加的字段,如果想讓animal的所有實(shí)例都擁有呢?

--使用Prototype
function animal(){}
animal.prototype.color= "green";
var cat = new animal();
var dog = new animal();
console.log(cat.color);//green
console.log(dog.color);//green 

通過(guò)Prototype不僅可以添加字段,還可以添加方法。

function animal(){}
animal.prototype.color= "green";
var cat = new animal();
var dog = new animal();
console.log(cat.color);//green
console.log(dog.color);//green
animal.prototype.run = funciton(){
console.log("run");
}
dog.run(); 

原來(lái)通過(guò)prototype屬性,在創(chuàng)建對(duì)象之后還可以改變對(duì)象的行為。
比如,可以為數(shù)組這個(gè)特殊對(duì)象添加一個(gè)方法。

Array.prototype.remove = function(elem){
var index = this.indexof(elem);
if(index >= 0){
this.splice(index, 1);
}
}
var arr = [1, 2, 3] ;
arr.remove(2); 

除了通過(guò)prototype為對(duì)象定義屬性或方法,還可以通過(guò)對(duì)象的構(gòu)造函數(shù)來(lái)定義類的屬性或方法。

function animal(){
this.color = "green";
this.run = function(){
console.log("run");
}
}
var mouse = new animal();
mouse.run(); 

以上做法的也可以讓所有的animal實(shí)例共享所有的字段和方法。并且還有一個(gè)好處是可以在構(gòu)造函數(shù)中使用類的局部變量。

function animal(){
var runAlready = false;
this.color = "green";
this.run = funciton(){
if(!runAlreadh){
console.log("start running");
} else {
console.log("already running")
}
}
} 

其實(shí),一個(gè)更加實(shí)際的做法是把通過(guò)構(gòu)造函數(shù)結(jié)合通過(guò)prototype定義一個(gè)類的字段和行為。

function animal(){
var runAlready = false;
this.run = function(){
if(!runAlready){
console.log('i am running');
} else {
console.log("i am already running");
}
}
}
animal.prototype.color = '';
animal.prototype.hide = funciton(){
console.log("");
}
var horse = new animal();
horse.run();
horse.hide(); 

Prototype允許我們?cè)趧?chuàng)建對(duì)象之后來(lái)改變對(duì)象或類的行為,并且這些通過(guò)prototype屬性添加的字段或方法所有對(duì)象實(shí)例是共享的。

相關(guān)文章

最新評(píng)論