Home > Web Front-end > JS Tutorial > body text

Javascript Study Notes: Objects (1): Use and Properties of Objects_Basic Knowledge

WBOY
Release: 2016-05-16 16:43:11
Original
1275 people have browsed it
false.toString(); // 'false'
[1, 2, 3].toString(); // '1,2,3'

function Foo(){}
Foo.bar = 1;
Foo.bar; // 1
Copy after login

An often misunderstood thing is that numeric constants cannot be regarded as objects. In fact, numeric constants can still be regarded as objects. This is because the Javascript parser makes a mistake when parsing the dot operator, treating it as a floating-point characteristic.

2.toString(); // raises SyntaxError

Actually, we have many ways to make a numeric constant behave as an object.

2..toString(); // the second point is correctly recognized
2 .toString(); // note the space left to the dot
(2).toString(); // 2 is evaluated first
Copy after login

Object as data type

Objects in Javascript can be used as hash tables, which mainly contain the correspondence between keys and values.
Use the {} symbol to create a simple object. This new object will inherit from Object.prototype and will not contain its own properties.

var foo = {}; // a new empty object
// a new object with a 'test' property with value 12
var bar = {test: 12}; 
Copy after login

Accessing the properties of an object

We can use two ways to access Javascript objects, namely the dot operator . and the square bracket operator [] .

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

var get = 'name';
foo[get]; // kitten

foo.1234; // SyntaxError
foo['1234']; // works
Copy after login

The effects of the two operators are almost the same. The only difference is that the bracket operator allows dynamic setting of attributes and the attribute name can have syntax errors. (The third situation in the above example has been explained)

Delete attributes of an object

The only way to delete an attribute is to use delete. Setting the attribute value to undefined or null only removes the value associated with the attribute and does not actually delete the attribute itself.

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]);
 }
}
Copy after login

The above outputs bar undefined and foo null, only baz is actually deleted.
One thing to note here is that delete can only delete attributes, not variables. Therefore, we must develop a good habit of writing var when defining variables. At any time, variables must be declared using the var keyword. Because if you don't write var, the variable will be mistakenly recognized as a new attribute created for the global object.

This example gives the answer quite clearly, a is a variable, and b is just a property of a global object.

Properties of named objects

var test = {
 'case': 'I am a keyword, so I must be notated as a string',
 delete: 'I am a keyword, so me too' // raises SyntaxError
};
Copy after login

Object properties can be named using ordinary characters or strings. Also due to a bug in the design of the Javascript parser, the second representation in the above example will throw an error in ECMAScript 5.
The reason for the error is that because delete is a keyword, it must be named using a string constant to adapt to the old version of the Javascript parser.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!