"JavaScript Advanced Programming Third Edition" Learning Notes (13) Advanced function __ Block chain
Source: Internet
Author: User
1. Safety of the primary type testThe native type of JS can be judged with typeof, but sometimes it will fail. For example, the typeof array returns not array, but object, and the old version of IE, which recognizes the function as object. Another type of judgment is instanceof, which can look up constructors in the object's prototype chain, but this approach can also cause problems with native types, because some native constructor users can be overwritten, such as array and JSON. Take a look at the following example:
[JavaScript]View plain copy function array () {this.type= "new array"; this.length=7; var a=new Array (); alert (a.length);//7 alert (typeof a);//object alert (a instanceof Array);//true var b=[1,2,3]; alert (b.length);//3 alert (b[1]);//2 alert (typeof b);//object alert (b instanceof Array);//false This is an interesting example, we cover the ARR Ay's constructor, and then creates an object A that is not an array, but instanceof recognizes it as an array, and instead, the B of array, is misjudged as another.
The way to solve this problem is to use ToString. The native type calls ToString and returns a string such as "[Object array]", even if the constructor is maliciously overwritten, the native type's ToString method is not overwritten.
[JavaScript]View Plain copy function Array () {this.length=7;} var a=1; var b=true; var c= "string"; var d=[]; var e=new Array (); Console.log (Object.prototype.toString.call (a));//[object number] Console.log (Object.prototype.toString.call (b)) ;//[object Boolean] Console.log (Object.prototype.toString.call (c));//[object String] Console.log ( Object.prototype.toString.call (d));//[object Array] Console.log (Object.prototype.toString.call (e));//[object Note: A function implemented as a COM object in old IE, which returns an object
2. Scoping Security constructorWhen we say object creation, we give a lot of methods to construct objects, and then we use those methods when we say inheritance. However, the methods of constructing objects are unsafe.
[JavaScript]View plain copy function person (name,age) {this.name=name; This.age=age; var p1=new person ("Brain", 18); var P2=person ("Brain", 18); alert (p1.name);//brain alert (p2);//undefined alert (window.name);//brain in the example above, there is a constructor person,p1 an object created using the correct invocation method. P2 uses an incorrect call. This incorrect invocation has a very serious side effect of adding the name attribute and the age attribute to the execution space of the person, and the current execution space is window. The idea to solve this problem is to make the constructor new and not new run the same result, which has a solution before, and now proposes a second solution.
[JavaScript] View Plain copy Function person (name,age) { if (this Instanceof person) { this
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