Basic knowledge of common programming knowledge in JavaScript

Source: Internet
Author: User

1.null and undefined

* Get a property from an object that has a value of undefined if the object and its prototype in the chain do not have the property.
* A function that returns the value to its caller without an explicit return value is undefined. One special case is when you are using new.
* A function in JavaScript can declare any form parameter, and when the function is actually called, the number of arguments passed in is less than the formal parameter of the declaration, then the value of the extra formal parameter is undefined.
* NULL is an empty object, notice the difference from the empty object ({}).
In short, for all variables, as long as the declaration has not specified the initial value, then he is undefined, if the object type is used to represent the concept of null reference, then is represented by NULL.

2.if expression

* NULL is always False (false)
* Undefined is always false (false)
* Number+0,-0 or NaN is false, other values are true
* String empty string is False, other value is true
* object is always true (true)

3.Array

Arrays in JavaScript are very different from the usual programming languages, such as Java or C + +. An object in JavaScript is an unordered associative array, and the array is implemented using this attribute of the object in JavaScript. In JavaScript, array is actually an object, except that its property name is an integer, and there are many additional attributes (such as length) and methods (such as splice) to manipulate the array conveniently.

4.new operator

JavaScript does not have the concept of classes in Java, but rather uses the constructor (constructor) to create objects. You can create a new object by using the constructor in the new expression. An object created by the constructor has an implied reference to the prototype of the constructor.


5.prototype

Prototype is the core concept of JavaScript prototype inheritance, and you must have seen the use of Array.prototype.push.call () in a JavaScript class library, so ultimately prototype is an object. We can add some useful methods to native class by prototype, or we can implement inheritance through prototype. If you are interested in prototype, you can access the prototype chain of the specified object through __proto__ in ff.

6.scope Chain

The execution context (execution) is an abstract concept used in the ECMASCRIPT specification to describe the execution of JavaScript code. All JavaScript code is run in an execution context. When a function is invoked in the current execution context, a new execution context is entered. When the function call ends, it is returned to the original execution context. If an exception is thrown during a function call and is not captured, it is possible to exit from multiple execution contexts. In the function call procedure, other function may also be invoked to enter the new execution context. This creates an execution context stack.

Note: If you are interested in scope chain, you can access the scope of the function through the __parent__ property in FF chain, it is more regrettable that the FF JS engine SpiderMonkey support for this property is not very perfect, there will be errors in the internal function , so it is recommended to use Rhino (Http://developer.mozilla.org/en/docs/Rhino).

Note: Here you need to note the effect of function expressions and function declarations on scope chain.

7.closure

The closure is also considered as JS more commonly used features, in general we execute a function in Java, internal variables will be all recycled, but in JavaScript, we can use some methods, the execution of the function of the internal variables remain, and accessible, thus forming a closure.

Of course, there are two sides to everything, closures bring us benefits, but also bring a lot of trouble, such as a inadvertently produced a leak of memory, so we need to apply these technologies reasonably. If you want to study the closure, we suggest to see a few JS library (Prototype,jquery,mootools), which has some very classic closure applications, such as the Bind method, here is not much to repeat.

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.