Home >Web Front-end >JS Tutorial >Detailed explanation of examples of using JavaScript objects and attribute operations_Basic knowledge

Detailed explanation of examples of using JavaScript objects and attribute operations_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 16:57:41973browse

All variables in JavaScript are objects, with two exceptions: null and undefined.

Copy code 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 misconception is that number literals are not objects. This is due to a bug in the JavaScript parser, which attempts to parse dot operators as part of a floating-point literal value.

Copy code The code is as follows:

2.toString(); // Error: SyntaxError

There are many workarounds to make number literals look like objects.

Copy code The code is as follows:

2..toString(); // second one The dot sign can be parsed normally
2 .toString(); // Pay attention to the space before the dot sign
(2).toString(); // 2 is calculated first
 

Object as data type

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

A simple object can be created using the object literal syntax - {} -. This newly created object inherits from Object.prototype and does not have any custom properties.

Copy code The code is as follows:

var foo = {}; // An empty object

// A new object with 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 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['1234']; // works

The two syntaxes are equivalent, but the square bracket operator still works in the following two cases Valid - dynamically set attributes - the attribute name is not a valid variable name (Translator's Note: For example, the attribute name contains spaces, or the attribute name is a JS keyword)

Translator’s Note: In the JSLint syntax detection tool, the dot operator is a recommended practice.

Delete attributes

The only way to delete a property is to use the delete operator; setting a property to undefined or null does not actually delete the property, but only removes the association between the property and the value.

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

Syntax of attribute names

Copy code 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
};


The attribute name of an object can be declared using strings or ordinary characters. However, due to another incorrect design of the JavaScript parser, the second declaration method above will throw a SyntaxError before ECMAScript 5.

The reason for this error is that delete is a keyword in the JavaScript language; therefore, in order to run normally under lower versions of JavaScript engines, string literal declaration must be used.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn