易错JS

JS解析过程
      步骤1. 读入第一个代码段(js执行引擎并非一行一行地执行程序,而是一段一段地分析执行的)
      步骤2. 做词法分析和语法分析,有错则报语法错误(比如括号不匹配等)

  步骤3. 如果还有下一个代码段,则读入下一个代码段,重复步骤2
      步骤4. 对【var】变量和【function】定义做“预解析“(永远不会报错的,因为只解析正确的声明)
      步骤5. 执行代码段,有错则报错(比如变量未定义)

javascript的作用域是词法性的,函数运行在定义他们的作用域中,而不是运行在调用他们的作用域中。
this,谁调用它,就是谁。

实际不存在this的值为null的情况,因为当this的值 为null的时候,其值会被隐式转换为全局对象。

作用域
==========================================================
 var x = 1;
 function ScopeTest(){
    alert( x );   //发现函数内的声明
    var x = 'hello world';//改值(内部变量)
    alert( x );    
 }
 ScopeTest();

alert(x);

**变量声明提前 undefined
==========================================================
var list = [1,2,3];
function test(){
    console.log(list);//函数内部发现undefined
    if(typeof list=='undefined'){//即使在if里面申明,声明也会提前//无块级
        var list = [4];//提前申明list,这里赋值
    }
    console.log(list);
}
test();

console.log(list)
==========================================================

function a(i) {
        var i;//局部变量和形参同名,会指向同一个存储地址, 类似 var i = i;
        alert(i);
 };
 a(10);

==========================================================
function change() {
    console.log(typeof fn)
    function fn() {
        alert('hello')
    }
    var fn
}
change()
变量声明在顺序上跟在函数声明和形式参数声明之后,对于重复的声明视而不见。

http://blog.csdn.net/wangxiaohu__/article/details/7262255
==========================================================
function test(xxx){
  console.log(xxx);

  console.log(arguments[0])
  function xxx(){//相当于xxx = function(){},优先申明,同时修改了同名参数
  }

 var xxx = 123;//申明提前,但是是重复反申明
   console.log(xxx);

   console.log(arguments[0])
}
test(444);
==========================================================

function aa(){

  function bb(){}//bb声明的是aa内部的函数,相当于 var bb =fn

  cc = function(){}//隐式的声明一个全局函数

}

aa();

==========================================================
  var obj = {
            i: "test",
            m: function() {
                alert(this.i); //指向obj对象 实例,输出值test
                function B() {
                    alert(this.i); //输出值undefined
            }
               B(); //null.B() == window.B(); this->window
            }
        }
        obj.m();
==========================================================
function a(i) {
    alert(i);//var i = i;
    alert(arguments[0]); //形参和实参共进退
    var i = 2;
    alert(i);
    alert(arguments[0]);
};
a(10);

function b(x, y, a) {
        arguments[2] = 10;
            alert(a);
}
b(1, 2, 3);
==========================================================
var length = 10
function fn(){
    console.log(this.length)
}
var obj = {
    length: 5,
    method: function(fn) {
        fn();//fn作为全局对象window的一个属性被传进来,调用的时候,this指向window

  console.log(arguments);

  console.log(typeof arguments)//O

  console.log(arguments.constructor)

  console.log(arguments instanceof Array)
        arguments[0]() // this->arguments,为什么是1????????????? cy={a:1,b:2};console.log(cy.length);因为arguments是个类数组对象
    }
}
obj.method(fn)
==========================================================
~function() {
    console.log(typeof next) ;//下面只是一个自执行的函数表达式,不是申明函数。
    ~function next() {//
        console.log(typeof next) //在函数内部其函数名总是有意义的!!+_+
    }()
}()
==========================================================
var f = function foo(){

    return typeof foo; // foo是在内部作用域内有效
 };
 // foo在外部用于是不可见的
 typeof foo; // "undefined"
 f(); // "function"

==========================================================
function f(){ return f; }
console.log(new f() instanceof f);

console.log(typeof new f());//function,不是f的实例,不是object
==========================================================17
function a() {
    alert(this);
}
a.call(null);//如果是null,则会指向。。
==========================================================

var x = 1;

if(function f(){}){

  x += typeof f;//不同浏览器下表现不用,分IE 非ie

}console.log(x);

==========================================================

==========================================================



数值转换
==========================================================
var a = 0;
var b = -1;
var c = 1;
function assert (aVar) {
if (aVar==true)   //true 会被转化成1
alert(true);
else     alert(false);
}
assert(a) ;
assert(b) ;
assert(c) ;

6.If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
7.If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
==========================================================
var a = "undefined";
var b = "false";
var c = "";
非空字符串转换为布尔值true。
4.If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
5.If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
==========================================================
'10'<'9'
==========================================================
100['toString']['length']
==========================================================

正则
==========================================================
var a = /123/g; //new RegExp("123","g");
var b = "abc#123";
console.log(a.test(b));//true
console.log(a.test(b));//false
原来正则表达式中g标记有个lastIndex属性,
它声明的是上一次匹配文本之后的第一个字符的位置,
找不到可以匹配的文本时,它们会自动把 lastIndex 属性重置为 0。
exec() 类似

http://perfectionkills.com/javascript-quiz/

http://adamlu.googlecode.com/svn/trunk/js_quiz.html

http://www.alixixi.com/web/a/2010062560089.shtml


                      ,
                    _/((
           _.---. .'   `\
         .'      `     ^ T=
        /     \       .--'
       |      /       )'-.
       ; ,   <__..-(   '-.)
        \ \-.__)    ``--._)
         '.'-.__.-.
           '-...-'

原文地址:https://www.cnblogs.com/cy056/p/2956778.html