Common programming knowledge in javascript _ basic knowledge

Source: Internet
Author: User
Javascript is just a scripting language, but it is also a programming language that we need to learn. The following is a reprinted article about common programming knowledge in javascript. 1. null and undefined

* Get an attribute from an object. If the object and its prototype chain do not have this attribute, the attribute value is undefined.
* If a function does not explicitly return a value to its caller through return, the return value is undefined. A special case is when new is used.
* Functions in JavaScript can declare any form of parameter. When the function is actually called, if the number of input parameters is smaller than the declared form parameter, the value of the redundant form parameter is undefined.
* Null is an empty object. Note the difference between null and null objects.
To put it simply, as long as no initial value is specified for all variables after declaration, it is undefined. If the Object type is used to represent the concept of null reference, it is expressed by null.

2. if expression

* Null is always false)
* Undefined is always false)
* Number + 0,-0 or NaN is false, and other values are true.
* If the String is null, it is a false String, and other values are true.
* The Object is always true)

3. Array

Arrays in JavaScript are very different from common programming languages, such as Java or C/C ++. Objects in JavaScript are unordered joined arrays, and Array is implemented by using this feature of objects in JavaScript. In JavaScript, Array is actually an object, but its attribute name is an integer. In addition, there are many additional attributes (such as length) and methods (such as splice) to conveniently operate arrays.

4. new operator

JavaScript does not have the concept of class in Java, but uses constructor to create objects. You can use the constructor in the new expression to create a new object. The object created by the constructor has an implicit reference pointing to the prototype of the constructor.


5. prototype

Prototype is the core concept of javascript prototype inheritance. You must have seen Array. prototype. push. call () in a javascript class library. Therefore, prototype is an object. We can use prototype to add some useful methods to native class, or use prototype to implement inheritance. If you are interested in prototype, you can use _ proto _ in ff to access the prototype chain of the specified object.

6. scope chain

Execution context is an abstract concept used in ECMAScript to describe JavaScript code execution. All JavaScript code is run in an execution context. When the function is called 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, the system may exit from multiple execution contexts. In the function call process, other functions may also be called to enter the new execution context. Thus, an execution context stack is formed.

Note: If you are interested in scope chain, you can use the _ parent _ attribute in ff to access the scope chain of the function, unfortunately, ff's js engine SpiderMonkey's support for this attribute is not very perfect, there will be an error in the internal function, so it is recommended to use Rhino (http://developer.mozilla.org/en/docs/Rhino ).

Note: Pay attention to the impact of function expressions and function declarations on scope chain.

7. closure

Closures are also common features of js. Generally, all internal variables will be recycled after a function is executed in java. However, in javascript, we can use some methods, make the internal variables of the executed function still exist and accessible, thus forming a closure.

Of course, everything has two sides. When the closure brings us benefits, it will also bring a lot of trouble. For example, a memory leak occurs when you are not careful, therefore, we need to apply these technologies properly. If you want to learn about closures in depth, we suggest looking at several js libraries (prototype, jquery, and mootools), which have some classic closure applications, such as the bind method. I will not repeat them here.

Related Article

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.