小計

複製代碼

1. 數組

var arr = [];
arr[0] = 0;
arr[1] = 1;
arr.bar = 'bar';
arr.length; //2複製代碼

2. 函數定義

JavaScript是一種解釋型語言,函數聲明會在JavaScript代碼加載後、執行前被解釋,而函數表達式只有在執行到這一行代碼時纔會被解釋。javascript

在JS中有兩種定義函數的方式:java

1.  var func = function(){}數組

2.  function func(){} bash

var 方式定義的函數,不能先調用後聲明,只能先聲明後調用。 函數

function方式定義函數能夠先調用後聲明,由於存在函數變量定義提高。ui

兩者在乎義上沒有任何不一樣,但其解釋優先級不一樣: 後者會先於同一語句級的其餘語句。
即: this

var result = func();  
function func(){
  return 5
} 複製代碼

 不會出錯;而 spa

var result = func();  
var func = function(){
  return 5
}
複製代碼

 則會出錯。prototype

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"複製代碼
相關文章
相關標籤/搜索