Get the App
SLTechnology News&Howtos  ›  Development  › 

The object property operation looks like this

Shulou Source: shulou.com Published: 2022-06-02 10:38:07 09月14日 Update

In this issue, Xiaobian will bring you about the operation of object attributes. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

var mi = { name: 'Xiaoming', birth: 1990, school: 'No.1 Middle School', //if special characters are quoted in quotes eg: 'school-name':'xxx'mi ['school-name'] height: 1.70, weight: 65, core: null}; The first property acquisition method: mi ['name ']; // ' Xiaoming 'The second property acquisition method: mi.name; // 'Xiaoming'

Access mi's name attribute, mi.name is written more concisely. When we write JavaScript code, we try to use standard variable names for attribute names, so that we can directly access an attribute in the form of object. prop.

You can also add and delete attributes

Add: mi.age; //undefined mi.age = 18; //Add an age attribute mi.age //Output 18 Delete: delete mi.age;mi.age //undefined

If we want to check whether xiaoming has an attribute, we can use the in operator:

'name' in mi; // true'grade' in mi; // false

Be careful, though, if in judges that an attribute exists, this attribute may not necessarily be mi's, it may be inherited by mi:

'toString' in mi; // true

Because toString is defined in the object object, and all objects eventually point to object on the prototype chain, mi also has the toString attribute.

To determine whether a property is owned by mi itself rather than inherited, use the hasOwnProperty() method:

mi.hasOwnProperty ('name '); // truemi.hasOwnProperty ('toString'); // false The above is the object attribute operation shared by everyone. If there is a similar doubt, please refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Tags: Attributes methods Xiaoming content objects analysis special concise professional small and medium code rich in content writing prototypes variables characters that is quotation marks form pointing to Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Information Redmi Huawei Apple