Tutukoy sa Attribute ng Object ng JavaScript
- Previous Page JS Object Definition
- Next Page JS Object Methods
Ang propyedad ay ang pinakamahalagang bahagi ng anumang objekto ng JavaScript.
Propyedad ng JavaScript
Ang propyedad ay ang halaga na may kaugnayan sa objekto ng JavaScript.
Ang mga propyedad ng objekto ng JavaScript ay walang pagkakasunod-sunod.
Ang mga propyedad ay karaniwang maaaring ayusin, idagdag, o alisin, ngunit ang ilan ay nangunguna bilang readonly.
Pagpasok sa propyedad sa JavaScript
Ang paraan ng pagpasok sa propyedad ng objekto ay:
objectName.property // person.age
o
objectName["property] // person["age"]
o
objectName[expression] // x = "age"; person[x]
Ang ekspresyon ay dapat magkakaroon ng pagkakabanggit bilang pangalan ng propyedad.
Halimbawa 1
person.fname + " is " + person.age + " years old.";
Halimbawa 2
person["fname"] + " is " + person["age"] + " years old.";
JavaScript for...in loop
JavaScript for...in
Ang mga pangungusap na nagsasagawa ng paglalakad sa mga propyedad ng objekto.
Grammar
for (variable in object) { Ang paraan na dapat maisagawa }
for...in
Ang bloke ng paraan sa lupon ay magsasagawa ng isang beses para sa bawat propyedad.
Lupon ng propyedad ng objekto:
Mga halimbawa
var person = {fname:"Bill", lname:"Gates", age:62}; for (x in person) { txt += person[x]; }
Magdagdag ng bagong propyedad
Maaari kang magdagdag ng bagong propyedad sa umiiral na objekto sa pamamagitan ng simple na pagtatalaga.
Pag sabihin na ang objekto ng person ay umiiral - madaling magdagdag ka ng bagong propyedad sa kanya:
Mga halimbawa
person.nationality = "English";
Hindi mo maaaring gamitin ang mga pinagiralang salita bilang pangalan ng propyedad (o pangalan ng paraan). Gamitin ang patakaran ng pangalan sa JavaScript.
Alisin ang propyedad
delete
Alisin ang mga keyword mula sa propyedad ng objekto:
Mga halimbawa
var person = {fname:"Bill", lname:"Gates", age:62}; delete person.age; // Or delete person["age"];
delete
Keywords will delete both the value of the property and the property itself at the same time.
After deletion, the property cannot be used until it is added back.
delete
Operators are designed for object properties. They have no effect on variables or functions.
delete
Operators should not be used for predefined JavaScript object properties. This can cause the application to crash.
Property Value
All properties have names. In addition, they have values.
The value is one of the attributes of the property.
Other attributes include: enumerable, configurable, writable.
These attributes define how properties are accessed (are they readable or writable?).
In JavaScript, all properties are readable, but only the value is modifiable (only when the property is writable).
(ECMAScript 5 has methods to get and set all property attributes)
Prototype Properties
JavaScript objects inherit their prototype's properties.
delete
Keywords do not delete inherited properties, but if you delete a prototype property, it will affect all objects that inherit from the prototype.
- Previous Page JS Object Definition
- Next Page JS Object Methods