Usage of javascript objects and examples of attribute operations _ basic knowledge

Source: Internet
Author: User
This article describes how to use javascript objects and how to operate attributes. For more information, see if all the variables in JavaScript are objects, except for null and undefined.

The Code is as follows:


False. toString (); // 'false'
[1, 2, 3]. toString (); // '1, 2, 3'

Function Foo (){}
Foo. bar = 1;
Foo. bar; // 1

A common misunderstanding is that the literal value of a number (literal) is not an object. This is because of an error in the JavaScript parser, which tries to resolve the vertex operator to a part of the floating-point numeric nominal value.

The Code is as follows:


2. toString (); // error: SyntaxError

There are many workways to make the literal value of A number look like an object.

The Code is as follows:


2. toString (); // The second vertex can be parsed normally.
2. toString (); // note the space before the dot
(2). toString (); // 2 is calculated first

 

Object as Data Type

JavaScript objects can be used as hash tables. They are mainly used to store the correspondence between named keys and values.

You can use the literal syntax-{}-of an object to create a simple object. This newly created Object inherits from Object. prototype and does not have any custom attributes.

The Code is as follows:


Var foo = {}; // an empty object

// A new object with a custom property 'test' with a value of 12'
Var bar = {test: 12 };

Access attributes

There are two ways to access the attributes of an object, the dot operator or the brackets operator.

The Code is as follows:


Var foo = {name: 'kitten '}
Foo. name; // kitten
Foo ['name']; // kitten

Var get = 'name ';
Foo [get]; // kitten

Foo.1234; // SyntaxError
Foo ['20140901']; // works


The two syntaxes are equivalent, but the brackets operator is still valid in the following two cases-dynamic attribute setting-attribute name is not a valid variable name (note: for example, the attribute name contains spaces or the attribute name is a JS keyword)

Note: In JSLint syntax detection tools, point operators are recommended.

Delete attributes

The only way to delete an attribute is to use the delete operator. Setting an attribute to undefined or null does not actually delete an attribute, but simply removes the association between attributes and values.

The Code is as follows:


14
Var obj = {
Bar: 1,
Foo: 2,
Baz: 3
};
Obj. bar = undefined;
Obj. foo = null;
Delete obj. baz;

For (var I in obj ){
If (obj. hasOwnProperty (I )){
Console. log (I, ''+ obj [I]);
}
}


The above output results include bar undefined and foo null-only baz is actually deleted, so it disappears from the output results.

Attribute name syntax

The Code is as follows:


Var test = {
'Case': 'I am a keyword so I must be notated as a string ',
Delete: 'I am a keyword too so me' // error: SyntaxError
};


Object attribute names can be declared using strings or common characters. However, due to another incorrect design of the JavaScript parser, the above second declaration method will throw the SyntaxError error before ECMAScript 5.

The cause of this error is that delete is a keyword of the JavaScript language. Therefore, to run normally in a lower version of the JavaScript engine, the string literal value declaration method must be used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.