JavaScript revelation, javascript revelation pdf

Source: Internet
Author: User

JavaScript revelation, javascript revelation pdf
I recently read the JavaScript revelation, a thin but informative book. Demonstrate an accurate JavaScript world view, involving objects, attributes, complex values, original values, scopes, inheritance, this keywords, head objects, and other important concepts.
1. JavaScript Object 1. javaScript pre-packages nine native Object constructors: Number (), String (), Boolean (), Object (), Array (), Function (), Date (), RegExp (), Error ()

Note: Math is a static object. It is not a constructor using the new operator.

2. the literal syntax has the same effect as the new operator. But there are also exceptions: Number (), String (), Boolean ()

When a string, number, or Boolean value uses a literal value, the actual complex object is created only when the value is regarded as an object. In other words, the original data type is always used before methods or attributes related to constructors are used. In this case, JavaScript creates a package object for the literal and treats the value as an object. After the method is called, JavaScript discards the package object. This value returns the literal type.

3. Original values: 10, true, null, undefined, and "123" original values are copied by actual values, and comparison is performed by comparing values.
Example:
Var myStr1 = "foo"; var myStr2 = myStr1; var myStr3 = "foo"; console. log (myStr2 === myStr3); // result: truemyStr1 = "test"; console. log (myStr1, myStr2); // result: test foo
4. complex values: Object (), Array (), Function (), Date (), RegExp (), Error ()

The copy operation of complex values refers to the copy of the reference, rather than the actual value; The comparison operation uses the reference comparison.

Example 1:
Var obj1 = {name: "ligang"}; var obj2 = obj1; var obj3 = {name: "ligang"}; console. log (obj1 === obj3); // falseobj1.age = 25; console. log (obj1 === obj2); // trueconsole. log (obj1, obj2); // Object {name: "ligang", age: 25} Object {name: "ligang", age: 25} obj1 = {name: "lg"}; // obj1 points to the new object, and obj2 no longer points to the same object console. log (obj1, obj2); // Object {name: "lg"} Object {name: "ligang", age: 25}
Ii. Object () and Funcation () 1. We do not recommend using the following methods. You can use this method to construct objects and functions! (1)
var obj1 = new Object('foo');var obj2 = new Object(function(){});
(2)
var add = new Function('param1', 'param2', 'return param1 + param2');var sub = new Function('param1, param2', 'return param1 - param2');
2. There are three different methods for defining a function: constructor, function statement, and function expression.
var addConstructor = new Function('x' ,'y' ,'return x + y');function addStatement(x, y){ return x + y; }var addExpression = function(x, y){ return x + y; };
3. The length of the real parameter and the form parameter
Var myFun = function (x, y, z) {console. log (myFun. length); // number of parameters: 3console. log (arguments. callee. length); // number of parameters: 3console. log (arguments. length); // number of real parameters: 2} myFun (1, 2 );
3. reference the head object in a web browser (1) name assigned to the head object: globalObj = window;
(2) Use the this keyword in the global scope: globalObj = this;
4. Inheritance of JavaScript prototype objects is actually an inheritance of the prototype chain! -- JavScript prototype (JavaScript you Don't know)
When an object does not contain an attribute, JavaScript searches the prototype chain.
Var ary = ['foo', 'bar']; ary. join (); // prototype chain: Array. prototypeary. toLocalString (); // prototype chain: Object. prototype
1. The default prototype attribute is an example of an Object:
var myFun = function(){}console.log(myFun.prototype);//firefox:Object {}chrome:myFun {}console.log(typeof myFun.prototype);//Object
2. The prototype is an object. The prototype chain links each instance to the prototype attribute of its constructor. This means that when any new keyword (or original value is used to create an object) is used to create an object, it adds a hidden link between the prototype attribute of "created object instance" and "created object constructor. This link is called _ proto _ In the instance __. Example:
Array. prototype. foo = 'foo'; var ary = []; ary. _ proto _. foo; // Result: "foo"
Note: __proto _ is not part of the official ECMA standard. The constructor attribute can be used to track links to prototype objects inherited from an object!
Example:
Array. prototype. bar = 'bar'; var arr = new Array (); console. log (arr. constructor. prototype. bar); // Result: "bar" console. log (arr. bar); // Result: "bar" is not displayed on the bar object. It is obtained from the inherited prototype.
3. Replacing the prototype property with a new object will delete the default constructor property example:
Var Foo = function Foo () {}; var FooInstance1 = new Foo (); console. log (FooInstance1.constructor); // result: Foo () Foo. prototype = {}; var FooInstance2 = new Foo (); console. log (FooInstance2.constructor); // result: Object ()

To replace the default prototype attribute set by JavaScript, reconnect the constructor attribute that references the constructor.

Example:
Var Bar = function Bar () {}; Bar. prototype = {constructor: Bar}; var BarInstance = new Bar (); console. log (BarInstance. constructor); // result: Bar ()
4. Replacing the prototype property with a new object will not update the previous instance example:
Var MyFun = function myFun () {}; MyFun. prototype. x = 1; var o1 = new MyFun (); MyFun. prototype = {x: 2} var o2 = new MyFun (); console. log (o1.x); // result: 1console. log (o2.x); // result: 2

Underscore. js: Enhancement and extension object
Backbone. js: provides models, collections, and views for complex Javascript applications.
Please indicate the source of reprint: http://blog.csdn.net/ligang2585116

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger. Reprint please indicate the source: http://blog.csdn.net/ligang2585116!

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.