JavaScript object usage and attribute manipulation Examples _ basics

Source: Internet
Author: User
Tags access properties

All variables in JavaScript are objects except for two exceptions null and undefined.

Copy Code code as follows:

False.tostring (); ' False '
[1, 2, 3].tostring (); ' 1,2,3 '

function Foo () {}
Foo.bar = 1;
Foo.bar; 1

A common misconception is that the literal value of a number (literal) is not an object. This is because of an error in the JavaScript parser, which attempts to resolve the point operator as part of the floating-point number face value.

Copy Code code as follows:

2.toString (); Error: SyntaxError

There are many workarounds to make the literal value of a number look like an object.

Copy Code code as follows:

2..toString (); The second dot can be parsed correctly.
2. toString (); Notice the space in front of the dot number
(2). toString (); 2 is calculated first
 

Object as a data type

JavaScript objects can be used as hash tables, primarily to hold the corresponding relationship between named keys and values.

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.

Copy Code code as follows:

var foo = {}; An empty object

A new object that has a custom attribute ' test ' with a value of 12
var bar = {Test:12};

Access Properties

There are two ways to access the properties of an object, the dot operator, or the bracket operator.

Copy Code code as follows:

var foo = {name: ' Kitten '}
Foo.name; Kitten
foo[' name ']; Kitten

var get = ' name ';
Foo[get]; Kitten

foo.1234; SyntaxError
foo[' 1234 ']; Works

The two syntaxes are equivalent, but the bracket operator is still valid in the following two cases-dynamic setting Property-property name is not a valid variable name (translator Note: For example, the attribute name contains a space, or the property name is JS keyword)

Translator Note: In the JSLint grammar Detection Tool, the point operator is the recommended practice.

Delete attribute

The only way to delete a property is by using the delete operator, setting the property to undefined or null and not actually deleting the attribute, but simply removing the association of the property and the value.

Copy Code code 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 output above has bar undefined and foo null-only Baz is actually deleted, so it disappears from the output.

Syntax for property names

Copy Code code as follows:

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


The property name of an object can be declared using a string or a normal character. However, due to another error design of the JavaScript parser, the second declaration method above throws a syntaxerror error before ECMAScript 5.

The reason for this error is that delete is a key word in the JavaScript language; Therefore, in order to function properly under a lower version of the JavaScript engine, the string literal declaration method must be used.

Related Article

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.