小计

319 阅读1分钟

1. 数组

var arr = [];
arr[0] = 0;
arr[1] = 1;
arr.bar = 'bar';
arr.length; //2

2. 函数定义

JavaScript是一种解释型语言,函数声明会在JavaScript代码加载后、执行前被解释,而函数表达式只有在执行到这一行代码时才会被解释。

在JS中有两种定义函数的方式:

1.  var func = function(){}

2.  function func(){}

var 方式定义的函数,不能先调用后声明,只能先声明后调用。

function方式定义函数可以先调用后声明,因为存在函数变量定义提升。

二者在意义上没有任何不同,但其解释优先级不同: 后者会先于同一语句级的其他语句。
即: 

var result = func();  
function func(){
  return 5
} 

 不会出错;而 

var result = func();  
var func = function(){
  return 5
}

 则会出错。

3. new关键字

function Person() {  
  getName = function () {    
    console.log(1);  
  }  
  return this;
}
Person.getName = function () {  
  console.log(2);
}
Person.prototype.getName = function () {  
  console.log(3);
}
var getName = function () {  
  console.log(4);
}
function getName() {  
  console.log(5);
}

getName();  //4
Person.getName();  //2
new Person.getName(); //2
(new Person).getName(); //3
new Person().getName(); //3
getName();  //1

typeof Person;  //"function"
typeof new Person;  //"object"
typeof new Person();  //"object"