Several issues to be aware of in JavaScript

Source: Internet
Author: User
Tags closure

Several issues to be aware of in JavaScript

JavaScript is a weak language, she does not use a very cumbersome memory management, type definition and so on, so the learning JavaScript threshold is relatively low. Low threshold does not mean that the language is very simple, we will encounter all kinds of strange problems when using, some because of the browser compatibility caused, and some because of the JS grammar itself caused by, and some because of the ECMAScript standard changes caused by, in short, such a lot of problems, the following list

Some of the more easily overlooked points

  1. Switch's case judgment

var t = Event.keycode;switch (t) {case   ' + ':      alert ("yay!");      break;}

When KeyCode is 65 o'clock, you will find, eh? How Wood has alert! It needs to be clear here that switch uses the full equals "= = =" When judging, and the whole equals sign is the same as the data type at the time of comparison, where T is the number type, and ' 65 ' is a string.

  2. Strict mode This≠window

"Use strict", var global = (function () {    console.log (this);//undefined}) ();

Sometimes we need to use global to cache this global environment (which may be window or something else, such as having no window object in the worker, and self representing global). But in strict mode the function scope returns this as undefined, in general, we can get to the global object in the following way:

"Use strict", var global = (function () {    var t = new Function ("return this");    Console.log (t);}) ();

Or:

"Use strict", var global = (function () {    window. Eval ("this");    Console.log (t);}) ();

Because the new function is executed at the global scope, the global object is returned, and the following eval needs to be noted together, if the window is not handed in before Eval, It is in the function scope (JavaScript uses the function to separate scopes) and naturally does not return a window or global object. One thing to note about using function:

(function () {   var local = 1;   New Function ("Console.log (typeof local);") (); Logs undefined} ());

The new function works under the global scope chain, so it is not accessible to the local in the anonymous function ~

  3. Variable lift (hoisting)

var t = "global"; function foo () {    console.log (t);//undefined    
var t = "local";}

This is a commonplace problem, Var is best spread everywhere. So-called variable elevation, where there are two scopes, one is the global scope, he has two variables, T and foo below, and Foo points to the Foo scope, foo scope has a T variable, draw a diagram to show

[Global Scope]    | -----t   [String] Undefined, "global"    |-----foo [Reference] [foo scope][foo scope]    |-----t
   [string] Undefined, "local"

Just enter the global scope chain, the program scan to T and foo Two variables, so give this T assignment is undefined, after sweep finish, see T has value, so assign value "global", Foo Point [foo scope], then enter [foo scope], Continue scanning the variables under the scope chain of the function, and after discovering the target T, the assignment is undefined, at Console.log:

var t = "global"; function foo () {    var t;//equals var t = undefined;
    


}

The above example does not write the return result is the same, plus return, just to better express the variable to improve the existence of this action. The general recommended variable definition method:

function foo (A, B, c) {    var x = 1,        bar,        baz = "Something";}

A Var, followed by the definition of a string of variables.

Attached: Places to note in strict JavaScript mode (transfer from cobalt carbonate)

1. Variables must be declared before they can be used

"Use strict"; a=1; Missing VAR statement to make statement, so error
"Use strict"; var a=b=1; Error B not declared

2. Function declaration statements (not including expressions) are not allowed in ordinary blocks of code (excluding closures)

"Use Strict";(function () {  //closure is the function  func () {}) that allows the statement to be declared (); {  var f=function () {};//function-declaration expression allows functions  func () {};//function declaration statement in normal closure, error};

3. This in the closure does not point to the global object

' Use strict ';(function () {  alert (this);//Output undefined}) ();

4. Object properties and function parameters cannot be declared repeatedly

"Use strict"; var o={a:1,a:1};//this object defines two a attributes, so an error
"Use strict"; function func (a,a) {};//The two parameters of the functions are a, so the error

5. Eval has a scope similar to closures

"Use strict"; var a=1,b=1;eval ("var a=2"); Window.eval ("var b=2"); alert (a); Output 1 because the run a becomes the local variable of the eval scope alert (b); Output 2 Window.eval is still global scope

6. Callee and Caller properties cannot be used

"Use strict"; function func () {  return Arguments.callee;//Error callee cannot be used};func ();

7. With statement not available

"Use Strict", with ({});

8. Eight binary numeric constants not available

"Use strict"; var a=0999; Decimal, you can use Var b=0123; Eight forbidden, unable to use

9. Some invalid operation in normal mode becomes error

"Use strict"; var a=1;delete a;//error Cannot delete var declared variable
"Use strict"; var o={get a () {}};o.a=1;//error assigning value to read-only property

Simply summed up so much, recommended "sub-cobalt carbonate" Children's shoes blog, meticulous, profound content, blog entry: http://www.web-tinker.com

For a detailed introduction to JavaScript strict mode, please visit: MDN strict_mode

Several issues to be aware of in JavaScript

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.