Object - JavaScript - MDN Web Docs
文章推薦指數: 80 %
All objects in JavaScript are descended from Object ; all objects inherit methods and properties from Object.prototype , although they may ...
SkiptomaincontentSkiptosearchSkiptoselectlanguage給開發者的網頁技術文件JavaScriptJavaScript參考文件標準內建物件ObjectArticleActions正體中文(繁體)ThispagewastranslatedfromEnglishbythecommunity.LearnmoreandjointheMDNWebDocscommunity.語法描述Object建構式屬性Object建構式方法Object物件實體與Object原型物件範例規範瀏覽器相容性參見RelatedTopicsStandardbuilt-inobjectsObjectPropertiesObject.prototype.constructor(en-US)
已棄用
Object.prototype.__proto__Methods
已棄用
Object.prototype.__defineGetter__()(en-US)
已棄用
Object.prototype.__defineSetter__()(en-US)
已棄用
Object.prototype.__lookupGetter__()(en-US)
已棄用
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()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)Object.prototype.toString()(en-US)Object.prototype.valueOf()(en-US)Object.values()(en-US)語法描述Object建構式屬性Object建構式方法Object物件實體與Object原型物件範例規範瀏覽器相容性參見ObjectObject建構式可用於建立物件包裝(objectwrapper)。
語法//Objectinitialiserorliteral
{[nameValuePair1[,nameValuePair2[,...nameValuePairN]]]}
//Calledasaconstructor
newObject([value])
參數
nameValuePair1,nameValuePair2,...nameValuePairN
Pairsofnames(strings)andvalues(anyvalue)wherethenameisseparatedfromthevaluebyacolon.
value
任意值。
描述TheObjectconstructorcreatesanobjectwrapperforthegivenvalue.Ifthevalueisnullorundefined,itwillcreateandreturnanemptyobject,otherwise,itwillreturnanobjectofaTypethatcorrespondstothegivenvalue.Ifthevalueisanobjectalready,itwillreturnthevalue.
Whencalledinanon-constructorcontext,ObjectbehavesidenticallytonewObject().
也可以參考objectinitializer/literalsyntax.Object建構式屬性
Object.length
Hasavalueof1.
Object.prototype
AllowstheadditionofpropertiestoallobjectsoftypeObject.
Object建構式方法
Object.assign()
Createsanewobjectbycopyingthevaluesofallenumerableownpropertiesfromoneormoresourceobjectstoatargetobject.
Object.create()
Createsanewobjectwiththespecifiedprototypeobjectandproperties.
Object.defineProperty()
Addsthenamedpropertydescribedbyagivendescriptortoanobject.
Object.defineProperties()
Addsthenamedpropertiesdescribedbythegivendescriptorstoanobject.
Object.entries()(en-US)
實驗性質
Returnsanarrayofagivenobject'sownenumerableproperty[key,value]pairs.
Object.freeze()
Freezesanobject:othercodecan'tdeleteorchangeanyproperties.
Object.getOwnPropertyDescriptor()(en-US)
Returnsapropertydescriptorforanamedpropertyonanobject.
Object.getOwnPropertyDescriptors()(en-US)
Returnsanobjectcontainingallownpropertydescriptorsforanobject.
Object.getOwnPropertyNames()(en-US)
Returnsanarraycontainingthenamesofallofthegivenobject'sownenumerableandnon-enumerableproperties.
Object.getOwnPropertySymbols()(en-US)
Returnsanarrayofallsymbolpropertiesfounddirectlyuponagivenobject.
Object.getPrototypeOf()
Returnstheprototypeofthespecifiedobject.
Object.is()(en-US)
Comparesiftwovaluesaredistinguishable(ie.thesame)
Object.isExtensible()(en-US)
Determinesifextendingofanobjectisallowed.
Object.isFrozen()(en-US)
Determinesifanobjectwasfrozen.
Object.isSealed()(en-US)
Determinesifanobjectissealed.
Object.keys()
Returnsanarraycontainingthenamesofallofthegivenobject'sownenumerableproperties.
Object.preventExtensions()
Preventsanyextensionsofanobject.
Object.seal()(en-US)
Preventsothercodefromdeletingpropertiesofanobject.
Object.setPrototypeOf()(en-US)
Setstheprototype(i.e.,theinternal[[Prototype]]property)
Object.values()(en-US)
實驗性質
Returnsanarrayofagivenobject'sownenumerablevalues.
Object物件實體與Object原型物件AllobjectsinJavaScriptaredescendedfromObject;allobjectsinheritmethodsandpropertiesfromObject.prototype,althoughtheymaybeoverridden.Forexample,otherconstructors'prototypesoverridetheconstructorpropertyandprovidetheirowntoString()methods.ChangestotheObjectprototypeobjectarepropagatedtoallobjectsunlessthepropertiesandmethodssubjecttothosechangesareoverriddenfurtheralongtheprototypechain.屬性{{page('/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype','屬性')}}方法{{page('/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Object/prototype','方法')}}範例UsingObjectgivenundefinedandnulltypes下面例子儲存一個空物件至變數o
varo=newObject();
varo=newObject(undefined);
varo=newObject(null);
UsingObjecttocreateBooleanobjects下面例子儲存Boolean物件在o:
//equivalenttoo=newBoolean(true);
varo=newObject(true);
//equivalenttoo=newBoolean(false);
varo=newObject(Boolean());
規範SpecificationECMAScriptLanguageSpecification#sec-object-objects瀏覽器相容性BCDtablesonlyloadinthebrowser參見
Objectinitializer
Foundaproblemwiththispage?EditonGitHubSourceonGitHubReportaproblemwiththiscontentonGitHubWanttofixtheproblemyourself?SeeourContributionguide.Lastmodified:2022年9月20日,byMDNcontributors
延伸文章資訊
- 1Object.keys() - JavaScript - MDN Web Docs
Object.keys() 方法會回傳一個由指定物件所有可列舉之屬性組成的陣列,該陣列中的的排列順序與使用for...in 進行迭代的順序相同(兩者的差異 ...
- 2JS 常用API - Object.assign && Object.defineProperty - iT 邦幫忙
JS 原力覺醒Day27 - JS 常用API - Object.assign && Object.defineProperty. JavaScript 原力覺醒- 成為絕地武士之路系列第27 ...
- 3Object - JavaScript - MDN Web Docs
All objects in JavaScript are descended from Object ; all objects inherit methods and properties ...
- 4JavaScript Objects - W3Schools
JavaScript objects are containers for named values called properties. Object Methods. Objects can...
- 5Object.assign() - JavaScript - MDN Web Docs
注意: Object.assign() 不會在來源物件屬性的值為 null 或 undefined 的時候拋出錯誤。 範例. 複製物件. var obj = { a ...