js的prototype,JS中對于prototype的理解

 2023-10-06 阅读 21 评论 0

摘要:JS中的prototype是JS中比較難理解的一個部分 本文基于下面幾個知識點: 1 原型法設計模式 在.Net中可以使用clone()來實現原型法 原型法的主要思想是,現在有1個類A,我想要創建一個類B,這個類是以A為原型的,并且能進行擴展。我們稱B的原型為A。 2 javascript的方法可以分

JS中的prototype是JS中比較難理解的一個部分

本文基于下面幾個知識點:

1 原型法設計模式

在.Net中可以使用clone()來實現原型法

原型法的主要思想是,現在有1個類A,我想要創建一個類B,這個類是以A為原型的,并且能進行擴展。我們稱B的原型為A。

2 javascript的方法可以分為三類:

a -> 類方法

js的prototype、b -> 對象方法

c -> 原型方法

例子:

function People(name){  //對象屬性this.name=name; //對象方法 this.Introduce=function(){alert("My name is "+this.name); }}

?//類靜態方法

?People.Run=function(){ ?

  alert("I?can?run");

js中的prototype?}

 

?//原型方法

?People.prototype.IntroduceChinese=function(){?

?alert("我的名字是"+this.name);

}?

 

?//測試

javascript prototype,?var?p1=new?People("Windking");

p1.Introduce();//調用對象方法

People.Run();//調用靜態方法

p1.IntroduceChinese(); //調用原型方法

3. obj1.func.call(obj)方法

意思是將obj看成obj1,調用func方法,本來調用的是obj1的func方法,但是,傳入obj后,改變了上下文對象,就通過obj對象來調用ojb1的方法了

好了,下面一個一個問題解決:

js中prototype的作用?prototype是什么含義?

javascript中的每個對象都有prototype屬性,Javascript中對象的prototype屬性的解釋是:返回對象類型原型的引用。

A.prototype = new B();

理解prototype不應把它和繼承混淆。A的prototype為B的一個實例,可以理解A將B中的方法和屬性全部克隆了一遍。A能使用B的方法和屬性。這里強調的是克隆而不是繼承。可以出現這種情況:A的prototype是B的實例,同時B的prototype也是A的實例。

先看一個實驗的例子:

function baseClass(){this.showMsg = function(){alert("baseClass::showMsg");   }}function extendClass(){}extendClass.prototype = new baseClass();var instance = new extendClass(); instance.showMsg(); // 顯示baseClass:showMsg

我們首先定義了baseClass類,然后我們要定義extentClass,但是我們打算以baseClass的一個實例為原型,來克隆的extendClass也同時包含showMsg這個對象方法。

prototype如何理解,extendClass.prototype = new baseClass()就可以閱讀為:extendClass是以baseClass的一個實例為原型克隆創建的。

?

?

那么就會有一個問題,如果extendClass中本身包含有一個與baseClass的方法同名的方法會怎么樣?

下面是擴展實驗2:

function baseClass(){this.showMsg = function(){alert("baseClass::showMsg");   }}function extendClass(){this.showMsg =function (){alert("extendClass::showMsg"); } } extendClass.prototype = new baseClass(); var instance = new extendClass(); instance.showMsg();//顯示extendClass::showMsg

實驗證明:函數運行時會先去本體的函數中去找,如果找到則運行,找不到則去prototype中尋找函數。或者可以理解為prototype不會克隆同名函數。

js __proto__。那么又會有一個新的問題:

如果我想使用extendClass的一個實例instance調用baseClass的對象方法showMsg怎么辦?

答案是可以使用call:

extendClass.prototype = new baseClass();

var instance = new extendClass();

var baseinstance = new baseClass();

prototype詳解、baseinstance.showMsg.call(instance);//顯示baseClass::showMsg

這里的baseinstance.showMsg.call(instance);閱讀為“將instance當做baseinstance來調用,調用它的對象方法showMsg”

好了,這里可能有人會問,為什么不用baseClass.showMsg.call(instance);

這就是對象方法和類方法的區別,我們想調用的是baseClass的對象方法

最后,下面這個代碼如果理解清晰,那么這篇文章說的就已經理解了:

function baseClass(){this.showMsg = function(){alert("baseClass::showMsg");   }this.baseShowMsg = function() {alert("baseClass::baseShowMsg");}}baseClass.showMsg = function(){ alert("baseClass::showMsg static"); } function extendClass(){ this.showMsg =function (){ alert("extendClass::showMsg"); } } extendClass.showMsg = function(){ alert("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

?

typescript和js的區別?4.最后跟大家介紹一下,call和prototype克隆的關系。

?function A(name){

  this.name = name;

}

A.prototype.sayName = function(){

  alert(this.name);

js中type屬性、}

?function B(){}

B函數如何操作,才可以繼承A的屬性和方法,有些同學會說,new一個B的原型等于A的實例,也有人說通過call即可實現,剛開始我也是這么理解的,后來我發現錯了。

function B(name){

  A.call(this,name);//此時,B函數只是繼承了A函數的屬性name,并未繼承A的sayName方法。

}

B.prototype = new A("Jack");//單獨這一句話,只能說明B函數克隆了A函數的sayName方法,因此call和prototype一起使用才能繼承和克隆A函數的所有屬性和方法。

Call只會繼承對象本身的屬性和方法,而不會繼承原型中的方法。prototype本身的作用是克隆,而不是繼承。

對于這一點,對于初學者一定要注意,我去百度面試的時候,已經吃過這樣的虧了,下來之后,我寫了一個Demo,才發現我錯了,但是面試官也沒有告訴我是錯的。

?

轉載于:https://www.cnblogs.com/jacksoft/p/5013721.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/119723.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息