JavaScript basic Finishing (1)

Source: Internet
Author: User

Recently read the "JavaScript authoritative guide" This book, Idle to have to think of important knowledge to do some sorting, convenient for later review.

The most important type in JavaScript is an object, a collection of name/value pairs, or a collection of string-to-value mappings. The object is enclosed in {}.

A JavaScript object is a composite value: It is a collection of properties or named values. Through the "." Symbol to reference the property value. When the value of a property is a function, it is called a method. Called by OBJECT.M ().

The method in object.

Create an object with the direct amount of the object: (the object's direct amount is an expression, and each operation of the expression creates and initializes a new object)

var book={name: "JavaScript", the value of the//name property is "JavaScript"

Fat:true} The value of the//fat property is True

Through the "." or "[]" to access the object properties. There are two ways to access an object's properties, with "[]" access, which can be accessed with "[]" when the name of the property is variable or unknown, because the string value is dynamic and can be changed at run time, and the identifier is static and must be written to die in the program. If you use "." Access, then the property name must be known and unchanging.

Book.name//Results JavaScript

Book["fat"]//Result true

Book.author= "Flank"; Create a new property by assigning a value

book.content={}; {} is an empty object, it has no properties

JavaScript also supports arrays (lists indexed by numbers)--arrays are special objects

var t=[3,4]; //array with four values, delimited by "[]"

A[3]=5//Add a new element by assigning a value.

An array or object can contain another array or object

var points=[//array with two elements

{x:0,y:0},//Each element is an object

{X:1,y:1}

];

JavaScript is an object-oriented language. Not strictly speaking, this means that we do not use global definition functions to manipulate different types of values, and the data type itself can define methods to work with values.

A.sort (); The object-oriented version array of sort (a) is a special object (built-in object), so it has its own method.

We know that if the property of the book object does not exist, then the property that gets it is undefined. But if the book object is not defined, that is, "." The previous object or property is not defined,

Then you will get an error.

If you want to get the properties of an undefined object, an error will be

So how to avoid this error, of course, you can use the judgment statement. There is a concise form

var len=book && book.a && book.a.length

When the value of book is undefined, because undefined is a false value, && does not calculate the right operand. Returns the value of undefined, which is the value of Len.

If book is defined and is an object because the object is true, && calculates the right operand and returns the result of the right operand as the value of Len.

Therefore, only book,book.a are defined (existing) in this expression, and the value of length is obtained, otherwise undefined is obtained.

In the above example, we see that the book has only one property a, and the "a" in book statement is used to determine whether property A is a property of book.

So why is ToString also the property of book? Where did ToString come from? This inheritance property is a bit complicated.

when using real numbers in JavaScript, it is often just an approximate representation of the real value.

In the real world of JavaScript

  

The properties of global objects are globally defined symbols that JavaScript programs can use directly. When the JavaScript interpreter starts (or when any web browser loads a new page),

It creates a new global object and gives it a set of defined initial properties:

Global properties, such as Undefined, infinity, and Nan

Global functions such as isNaN (), parseint (), and Eval ()

constructors, such as date (), RegExp (), String (), Object (), Array ()

Global objects, such as math and JSON

If the code declares a global variable, this global variable is a property of the global object. (Everything is an object)

Here we ask that global variables are properties of global objects, so are local variables also properties of objects?

Scope chain-this is important.

objects are not just string-to-value mappings, but, in addition to preserving their own properties, JavaScript objects can inherit properties from an object called a prototype. The method of the object is usually

The inherited property. This "archetypal inheritance" is a core feature of JavaScript. (This sentence is not very understanding)

Create an object with new (built-in constructor):

var o=new Object (); Create an empty object, as with {}

var a=new Array (); Create an empty array, as in []

var r=new RegExp ("JS"); Create a RegExp object that can be pattern-matched

Functions--Special objects

In JavaScript, functions are objects, and programs can manipulate them arbitrarily. For example, JavaScript can assign a function to a variable, or pass it as a parameter to another function.

Because functions are objects, you can set properties on them and even invoke their methods.

In addition to the arguments, each invocation also has another value-the context of this invocation-this is the value of the This keyword.

Execute the function immediately:

JavaScript basic Finishing (1)

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.