Object.prototype.__proto__ - JavaScript - MDN Web Docs

文章推薦指數: 80 %
投票人數:10人

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



請為這篇文章評分?