Object.prototype.__proto__ - JavaScript - MDN Web Docs
文章推薦指數: 80 %
The __proto__ property of Object.prototype is an accessor property (a getter function and a setter function) that exposes the internal ...
SkiptomaincontentSkiptosearchSkiptoselectlanguage給開發者的網頁技術文件JavaScriptJavaScript參考文件標準內建物件ObjectObject.prototype.__proto__ArticleActions正體中文(繁體)ThispagewastranslatedfromEnglishbythecommunity.LearnmoreandjointheMDNWebDocscommunity.SyntaxDescriptionSpecificationsBrowsercompatibilityCompatibilitynotesSeealsoRelatedTopicsStandardbuilt-inobjectsObjectPropertiesObject.prototype.constructor(en-US)
Deprecated
Object.prototype.__proto__Methods
Deprecated
Object.prototype.__defineGetter__()(en-US)
Deprecated
Object.prototype.__defineSetter__()(en-US)
Deprecated
Object.prototype.__lookupGetter__()(en-US)
Deprecated
Object.prototype.__lookupSetter__()(en-US)Object.assign()Object.create()Object.defineProperties()Object.defineProperty()Object.entries()(en-US)Object.freeze()Object.fromEntries()(en-US)Object.getOwnPropertyDescriptor()(en-US)Object.getOwnPropertyDescriptors()(en-US)Object.getOwnPropertyNames()(en-US)Object.getOwnPropertySymbols()(en-US)Object.getPrototypeOf()
Experimental
Object.hasOwn()(en-US)Object.prototype.hasOwnProperty()Object.is()(en-US)Object.isExtensible()(en-US)Object.isFrozen()(en-US)Object.prototype.isPrototypeOf()(en-US)Object.isSealed()(en-US)Object.keys()Object.preventExtensions()Object.prototype.propertyIsEnumerable()(en-US)Object.seal()(en-US)Object.setPrototypeOf()(en-US)Object.prototype.toLocaleString()(en-US)
Non-Standard
Deprecated
Object.prototype.toSource()(en-US)Object.prototype.toString()(en-US)Object.prototype.valueOf()(en-US)Object.values()(en-US)SyntaxDescriptionSpecificationsBrowsercompatibilityCompatibilitynotesSeealsoObject.prototype.__proto__
Warning: 基於現代Javascript引擎最佳化物件屬性存取的方法,改變一個物件的 [[Prototype]] 在任何瀏覽器或是Javascript引擎都是非常慢的操作?。
改變繼承屬性對效能的影響微妙且深遠,不僅僅只是影響執行 obj.__proto__=... 的時間,而是會影響到所有有存取到被改變 [[Prototype]] 的物件的程式碼的執行時間。
如果你在乎效能的話就應該避免改變一個物件的 [[Prototype]] 。
反之,請用 Object.create()來產生一個擁有 [[Prototype]] 的物件。
Warning: 雖然 Object.prototype.__proto__ 在今日已經被絕大部分的瀏覽器所支援,其存在與確切的行為只有在 ECMAScript2015規範才被標準化成一個歷史功能來確保相容性。
為了更好的支援,建議使用Object.getPrototypeOf()。
The__proto__propertyofObject.prototypeisanaccessorproperty(agetterfunctionandasetterfunction)thatexposestheinternal[[Prototype]](eitheranobjectornull)oftheobjectthroughwhichitisaccessed.
Theuseof__proto__iscontroversial,andhasbeendiscouraged.ItwasneveroriginallyincludedintheEcmaScriptlanguagespec,butmodernbrowsersdecidedtoimplementitanyway.Onlyrecently,the__proto__propertyhasbeenstandardizedintheECMAScript2015languagespecificationforwebbrowserstoensurecompatibility,sowillbesupportedintothefuture.ItisdeprecatedinfavorofObject.getPrototypeOf/Reflect.getPrototypeOf(en-US)andObject.setPrototypeOf(en-US)/Reflect.setPrototypeOf(en-US)(thoughstill,settingthe[[Prototype]]ofanobjectisaslowoperationthatshouldbeavoidedifperformanceisaconcern).
The__proto__propertycanalsobeusedinanobjectliteraldefinitiontosettheobject[[Prototype]]oncreation,asanalternativetoObject.create().See:objectinitializer/literalsyntax.SyntaxvarCircle=function(){};
varshape={};
varcircle=newCircle();
//Settheobjectprototype.
//DEPRECATED.Thisisforexamplepurposesonly.DONOTDOTHISinrealcode.
shape.__proto__=circle;
//Gettheobjectprototype
console.log(shape.__proto__===circle);//true
varshape=function(){};
varp={
a:function(){
console.log('aaa');
}
};
shape.prototype.__proto__=p;
varcircle=newshape();
circle.a();//aaa
console.log(shape.prototype===circle.__proto__);//true
//or
varshape=function(){};
varp={
a:function(){
console.log('a');
}
};
varcircle=newshape();
circle.__proto__=p;
circle.a();//a
console.log(shape.prototype===circle.__proto__);//false
//or
functiontest(){};
test.prototype.myname=function(){
console.log('myname');
};
vara=newtest();
console.log(a.__proto__===test.prototype);//true
a.myname();//myname
//or
varfn=function(){};
fn.prototype.myname=function(){
console.log('myname');
};
varobj={
__proto__:fn.prototype
};
obj.myname();//myname
Note:thatistwounderscores,followedbythefivecharacters"proto",followedbytwomoreunderscores.DescriptionThe__proto__getterfunctionexposesthevalueoftheinternal[[Prototype]]ofanobject.Forobjectscreatedusinganobjectliteral,thisvalueisObject.prototype.Forobjectscreatedusingarrayliterals,thisvalueisArray.prototype.Forfunctions,thisvalueisFunction.prototype.Forobjectscreatedusingnewfun,wherefunisoneofthebuilt-inconstructorfunctionsprovidedbyJavaScript(Array,Boolean,Date,Number,Object,String,andsoon —includingnewconstructorsaddedasJavaScriptevolves),thisvalueisalwaysfun.prototype.Forobjectscreatedusingnewfun,wherefunisafunctiondefinedinascript,thisvalueisthevalueoffun.prototype.(Thatis,iftheconstructordidn'treturnanotherobjectexplicitly,orthefun.prototypehasbeenreassignedsincetheinstancewascreated).
The__proto__setterallowsthe[[Prototype]]ofanobjecttobemutated.TheobjectmustbeextensibleaccordingtoObject.isExtensible()(en-US):ifitisnot,aTypeError(en-US)isthrown.Thevalueprovidedmustbeanobjectornull.Providinganyothervaluewilldonothing.
Tounderstandhowprototypesareusedforinheritance,seeguidearticleInheritanceandtheprototypechain.
The__proto__propertyisasimpleaccessorpropertyonObject.prototypeconsistingofagetterandsetterfunction.Apropertyaccessfor__proto__thateventuallyconsultsObject.prototypewillfindthisproperty,butanaccessthatdoesnotconsultObject.prototypewillnotfindit.Ifsomeother__proto__propertyisfoundbeforeObject.prototypeisconsulted,thatpropertywillhidetheonefoundonObject.prototype.SpecificationsSpecificationECMAScriptLanguageSpecification#sec-additional-properties-of-the-object.prototype-objectBrowsercompatibilityBCDtablesonlyloadinthebrowser
CompatibilitynotesWhiletheECMAScript2015 specificationdictatesthatsupportfor__proto__isrequiredonlyforwebbrowsers(althoughbeingnormative),otherenvironmentsmaysupportitaswellforlegacyusage.Seealso
Object.prototype.isPrototypeOf()(en-US)
Object.getPrototypeOf()
Object.setPrototypeOf()(en-US)
Foundaproblemwiththispage?EditonGitHubSourceonGitHubReportaproblemwiththiscontentonGitHubWanttofixtheproblemyourself?SeeourContributionguide.Lastmodified:2022年6月4日,byMDNcontributors
延伸文章資訊
- 1prototype中文(繁體)翻譯:劍橋詞典
prototype的例句. prototype. The ability to distinguish between prototypes and exceptions helps to de...
- 2原型兵器- 維基百科,自由的百科全書
《原型兵器》(英語:Prototype,中國大陸譯作「虐殺原形」)是由Radical Entertainment公司製作,美國動視發行於2009年6月9日發售的開放式科幻自由動作冒險遊戲,玩家 ...
- 3物件原型- 學習該如何開發Web | MDN
JavaScript 的物件即透過原型(Prototype) 機制相互繼承功能,且與典型的物件導向(OO) 程式語言相較,其運作方式有所差異。我們將透過本文說明相異之 ...
- 4prototype - 雛型 - 國家教育研究院雙語詞彙
出處/學術領域, 英文詞彙, 中文詞彙. 學術名詞 視覺藝術名詞, prototype, 原型. 學術名詞 圖書館學與資訊科學名詞, prototype, 原型. 學術名詞
- 5prototype - Yahoo奇摩字典搜尋結果
prototype. KK[ˋprotə͵taɪp]; DJ[ˋprəutətaip]. 美式. n. 原型;標準;模範. Dr.eye 譯典通 · prototype · 查看更多. IPA[...