JavaScript Learning Notes Object (i): Object usage and Attributes _ basics

Source: Internet
Author: User
False.tostring (); ' False '
[1, 2, 3].tostring ();//' 1,2,3 '

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

One oft-misunderstood thing is that numeric constants cannot be treated as objects, and numeric constants can still be treated as objects. This is because the Javascript parser makes the error of resolving the point operator as a floating-point feature.

2.toString (); Raises syntaxerror

In fact, there are many ways we can make a numeric constant behave as an object.

2..toString (); The second point is correctly recognized
2. ToString ()///Note the "space" to the Dot
(2). toString ();//2 is evaluated

Object as a data type

Objects in Javascript can be used as hash tables, which mainly contain the corresponding relationship between the key and the value.
Use the {} symbol to create a simple object that will inherit from Object.prototype and not contain its own defined attributes.

var foo = {}; A new empty object
//A new object with a ' test ' property with value 12

Accessing the properties of an object

There are two ways we can access Javascript objects, which are point operators, respectively. and the bracket operator [].

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

= ' name ';
Foo[get]; Kitten

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

The effect of the two operators is almost the same, the only difference being that the bracket operator allows the property to be dynamically set and the property name can have syntax errors. (The third case in the example above is described)

Delete an object's properties

The only way to delete a property is by using Delete, setting the property value to undefined or null simply removing the value associated with the attribute and not actually deleting the property 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]);
 }

The above output bar undefined and foo null, only Baz is actually deleted.
To illustrate, delete can only delete attributes and cannot delete variables. So when we define variables, we must develop a good habit of writing Var, at any time, the variable must use the VAR keyword to declare. Because if Var is not written, the variable is mistakenly recognized as creating a new attribute for the global object.

This example quite clearly gives the answer, 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 to be notated as a string ',
 Delete: ' I am a keyword, so me too '/RA Ises SyntaxError
};

The properties of an object can be named with ordinary characters or strings. Also due to an error design of the Javascript parser, the second representation in the example above will throw an error in ECMAScript 5.
The reason for the error is that delete is a keyword, so you must use a string constant to name it to fit the old version of the Javascript parser.

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.