7.2オブジェクトの属性(Object Properties)
2221 ワード
オブジェクトの属性値はオペレータによってアクセスできます.オペレータの左の値はオブジェクトでなければなりません.通常はオブジェクト参照を含む変数の名前です.オブジェクトを返すためのjavascriptの表現です.オペレータの右の値は識別子でなければなりません.文字列や表現ではいけません.
Object properties work like variables:you can store values in them and read values from them.
(オブジェクトの属性は変数の働き方に似ています.)
var book={}//Set a property in the object.book.title=「JavaScript:The Definitive Gide」//Set some more properties.Note the nese ted object.book.chapter 1=new Object();book.chapter 1.title=「Introduction to JavaScript」book.chapter 1.pages=11;book.chapter 2={title:「Lexical Structure」、pages:6}.Read some property values from the object.alert(「Outline:」+book.title+「\t」+ 「Chapter 1」+book.chapter 1.title+「\t」+ 「Chapter 2」+book.chapter 2.title);
属性の列挙
for/inでオブジェクトを巡回、列挙するすべての属性を使用できます.
例:
function DisplayPropertyNames(obj){ var names=" for(var name in obj)names+=name+"; alert(names);
Note:for/inサイクルリストの属性は特定の順序がなく、すべてのユーザが定義した属性を列挙することができますが、予め定義された属性や方法は列挙できません.
属性が存在するかどうかをチェックする(Checking Property Existence)
属性が存在するかどうかは、inオペレータで検出することができます.
The left side of this operator shound be the name of the property as a sting.The right side shound be the object to be tested
if("x"in o)o.x=1;
if you query a property that does not exist,the undefined value is returned
if(o.x!==undefined)o.x=1;
Note that it is possible for a property to exist but still be undefined.For example,if you write:
o.x=undefined
Note also that the!==operator was used earlier instead of the more comon!=operator.!==and==distinguish between undefined and null
//If the doSomething property exists and is not null or undefined/then asume it is a function and invoke it!if(o.doSomething)o.doSomething()
Deleting Propties
You can use the delete operator to delete a property of an object:
Note that deleting a property does not merrely set the property to undefined;it actually removes the property from the object.After deletion,for/in will not enumerate the property and the in operator will not detect it.
Object properties work like variables:you can store values in them and read values from them.
(オブジェクトの属性は変数の働き方に似ています.)
var book={}//Set a property in the object.book.title=「JavaScript:The Definitive Gide」//Set some more properties.Note the nese ted object.book.chapter 1=new Object();book.chapter 1.title=「Introduction to JavaScript」book.chapter 1.pages=11;book.chapter 2={title:「Lexical Structure」、pages:6}.Read some property values from the object.alert(「Outline:」+book.title+「\t」+ 「Chapter 1」+book.chapter 1.title+「\t」+ 「Chapter 2」+book.chapter 2.title);
属性の列挙
for/inでオブジェクトを巡回、列挙するすべての属性を使用できます.
例:
function DisplayPropertyNames(obj){ var names=" for(var name in obj)names+=name+"; alert(names);
Note:for/inサイクルリストの属性は特定の順序がなく、すべてのユーザが定義した属性を列挙することができますが、予め定義された属性や方法は列挙できません.
属性が存在するかどうかをチェックする(Checking Property Existence)
属性が存在するかどうかは、inオペレータで検出することができます.
The left side of this operator shound be the name of the property as a sting.The right side shound be the object to be tested
if("x"in o)o.x=1;
if you query a property that does not exist,the undefined value is returned
if(o.x!==undefined)o.x=1;
Note that it is possible for a property to exist but still be undefined.For example,if you write:
o.x=undefined
Note also that the!==operator was used earlier instead of the more comon!=operator.!==and==distinguish between undefined and null
//If the doSomething property exists and is not null or undefined/then asume it is a function and invoke it!if(o.doSomething)o.doSomething()
Deleting Propties
You can use the delete operator to delete a property of an object:
delete book.chapter2;
Note that deleting a property does not merrely set the property to undefined;it actually removes the property from the object.After deletion,for/in will not enumerate the property and the in operator will not detect it.