Javascript (ECMASCRIPT5) details and a counterintuitive place

Source: Internet
Author: User

This article is constantly updated by documenting some of the differences in learning JavaScript (ECMASCRIPT5) from other languages.

The knowledge inside may not be suitable for a JavaScript programmer with some experience, but is not limited to reading for beginners.

1. Null is a pointer to an object that represents "null"

var NULL Console.log (typeof Foo)  // output Object Instead of what you think is null

2. undefined is a null-derived

Console.log (null = = undefined)  // output True

3. Floating-point numerical calculation error problem

var a = 0.1= 0.3if (a+b = = 0.3) {     alert (' OK ')  /// However a+b will be equal to 0.300000000000000004}

Of course not only ECMAScript have this problem, many use the same numeric type also has

4.NaN is not equal to any value, including its own

// false // false // false // of course, you can use the IsNaN function

The 5.for in statement is a delivery without order

6.switch can be any object comparison, unlike C + + or Java 7 cannot be a string comparison

7. Parameter parameters for all functions are passed by value, even if the object is passed in, the reference pointer is copied

function setName (obj) {  // This obj is a reference pointer to the copy, pointing to a     obj.name =       ' Onename ' New // b  But now you've changed this copy of the reference pointer to b     obj.name = ' twoname '    // set just B}  varnew//  A//  output Onename

8.Javascript no block-level scope (closures are not discussed for the time being)

You might think that the two {} braces belong to the scope area, which is the Java experience, but it's not implemented in JavaScript.

if (true) {     var name = ' youname '//  can still get output younamefor (var i=0;i<5;i++) {     i++// can still get variable i

9. Objects defined using object literal notation will not actually call the object constructor, which is recommended only for read-only business use.

var // The result is the same as the new Object (), but the constructor is not called // can still operate like a class var obj = {    ' Name '    function() {//foo}//  Same

10. Point directly to the object, not to the next reference

var function () {    alert (' Run ');} var C =null;    // is still working and does not affect

11. What does the riddle of this pointer actually point to?

functionfunc (A, b) {alert ( This)       returnA +B;}varA = 1;varB = 2; func (A, b);//invoke function alert Output window//In fact, this is the equivalent of calling this value by default to bind to the globalFunc.call (WINDOW,A,B)//Alert Output WindowFunc.call (UNDEFINED,A,B)//Alert output undefinedFunc.apply (NewObject (), [A, b])//Output Object
This first parameter is the This value

Executing a function in a different environment will get a different this value, here is the global environment, so this is bound to the global object, so the direct call will be window, and if you call it in any other environment then it will be the other value.

If you do this, the function has a owning object, and this is bound to the owning object.

var New  function() {console.log (this// ) is called, at which point the this value is bound to obj, which is the obj

The so-called binding, in fact, is the function of all bind () functions.

var o = {color: ' Red '}; function Saycolor () {    Console.log (this. color);} Window.color= ' undefined 'var a =  //Once you have this explicit binding, no matter what the environment, it must be the object you are bound to is thisa  () // this will output red instead of the global object color undefined

Let's go back to this topic:

case One: normal function call (func (A,b,c,d,....);) This is bound to the global Globals object.

Scenario Two: Invocation as an object method (Obj.func (A,b,c,...)) This binds to the object on which it is attached.

Scenario Three: called as a constructor (var obj = new Yourobject ()) This is bound to the newly constructed object (obj) that appears.

situation four: Show bindings (bind apply call, etc.) this binds to the other person you specify.

12. Attributes override the properties of the prototype, and when the property is deleted, the properties of the prototype are restored again

Array.prototype.name = "A"; var New  // output aobj.name = "B"obj.name// output BDelete   // output A instead of undefined

13. Fully rewritten prototypes are not reflected on existing instances

var function  = {  name:' You '  }varnew//  output you  // then we rewrite the prototype a.prototype = {// This modified prototype will not be applied to the prototype that has already been instantiated  name: ' ME '  // still output you

Javascript (ECMASCRIPT5) details and a counterintuitive place

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.