javascript變數聲明,hoisting機制,物件導向

來源:互聯網
上載者:User

標籤:

 以下不遵守可能引起未知錯誤

1.  表示區塊起首的花括弧,不要另起一行。

2.  不要省略句末的分號

3.  不要使用with語句,可以減少代碼的書寫,但是會造成混淆。

4.  不要使用"相等"(==)運算子,只使用"嚴格相等"(===)運算子。

== 兩邊實值型別不同的時候,要先進行類型轉換,再比較,造成很多意想不到的情況。

=== 不做類型轉換,類型不同的一定不等。

0 == ‘‘// true1 == true // true2 == true // false0 == ‘0‘ // truefalse == ‘false‘ // falsefalse == ‘0‘ // true" \t\r\n " == 0 // true

5.  所有變數申明都要寫在函數的頭部。所有函數都要在使用之前定義

因為javascript的函式宣告和變數聲明有一個hoisting機制,聲明永遠都會被提升至範圍的最頂端。

var a = 10; (function () {    alert(a);//undefined    var a = 20;})()//相當於:var a = 10;var f = function () {    alert(a);//undefined    var a = 20;}f()//相當於:var a = 10;function xx() {    alert(a);//undefined    var a = 20;}xx()//相當於:(function () {    var a;//在這裡對變數hoisting,先聲明    alert(a);    a = 20;//再賦值})()

    註:兩種函式宣告的var f = function () {}和function f() {} 不一樣

f();//有錯var f = function () {    alert("Hello");}//語句相當於:var f;f();f = function () {    alert("Hello");}

  

 

var b = (function () {    return 20;  }())與 (function () {    var a;//在這裡對變數hoisting,先聲明    alert(a);    a = 20;//再賦值})()

  

 安全沙箱模式

var f = function foo(){    return typeof foo;};typeof foo;// "undefined"f();// "function"

 函數名 foo 只在該函數的範圍內有用

JavaScript本身中沒有提供命名空間機制,為了避免不同函數、對象以及變數名對全域空間的汙染,通常的做法是為你的應用程式或者庫建立一個唯一的全域對象,然後將所有方法與屬性添加到這個對象

 

function Person(name, age) {    this.name = name;    this.age = age;    this.sayName = function () {        alert(this.name);    }}Person(1, 2)alert(name)//我們在函數的內部,可以使用this.XXX以及var來聲明變數。//區別是使用this.XXX聲明的變數在外部是可以訪問的。//使用var聲明的變數,由於受到函數範圍的保護,在函數的外部是無法直接存取的。

在JavaScript中,並沒有顯示的聲明私人成員的關鍵字等。所以要想實現封裝/資訊隱藏就需要從另外的思路出發。我們可以使用閉包的概念來建立只允許從對象內部訪問的方法和屬性,來達到封裝的要求。

基本方式一般來說,我們學用的有三種方法來達到封裝的目的。使用this.XXX來聲明一個變數,然後再聲明getXXX、setXXX等取值、賦值的方法。使用this._XXX來聲明一個變數,然後再聲明getXXX、setXXX等取值、賦值的方法。利用“函數範圍”這一個概念來做。

  

var Book = function(isbn,title,author){  this.setIsbn(isbn);  this.setTitle(title);  this.setAuthor(author);};Book.prototype = {  setIsbn: function(isbn){    this.isbn = isbn;  },  getIsbn: function(){    return this.isbn;  },  setTitle: function(title){    this.title = title;  },  getTitle: function(){    return this.title;  },  setAuthor: function(author){    this.author = author;  },  getAuthor: function(){    return this.author;  }};//使用這種方法實現的封裝,雖然實現了取值器與賦值器以保護私人屬性。但是在實際使用中,私人屬性依然可以從外部存取,所以從根本上講,沒有實現封裝。

  

var Book = function(isbn,title,author){  this.setIsbn(isbn);  this.setTitle(title);  this.setAuthor(author);};Book.prototype = {  setIsbn: function(isbn){    this._isbn = isbn;  },  getIsbn: function(){    return this._isbn;  },  setTitle: function(title){    this._title = title;  },  getTitle: function(){    return this._title;  },  setAuthor: function(author){    this._author = author;  },  getAuthor: function(){    return this._author;  }};//使用這種方法與第一種類似,區別在於使用不同的命名來保護私人屬性的使用。但是,從實際應用來說其仍然沒有實現封裝。

  

var Book = function(newIsbn,newTitle,newAuthor){  var isbn,title,author;  this.setIsbn=function(newIsbn){    isbn = newIsbn;  };  this.getIsbn=function(){    return isbn;  };  this.setTitle=function(newTitle){    title = newTitle;  };  this.getTitle=function(){    return title;  };  this.setIsbn=function(newAuthor){    author = newAuthor;  };  this.getIsbn=function(){    return author;  };}//由於在JavaScript的函數中聲明的變數是有範圍的,所以使用這種方法可以避免在外部直接存取私人屬性。基本達到封裝所要求的內容。

  

無論什麼時候,只要建立一個新函數,就會根據一組特定的規則為該函數建立一個prototype屬性,這個屬性指向函數的原型對象。申明函數Book,Book的屬性prototype的指向和Book內this指向一樣嗎http://www.cnblogs.com/sitemanager/p/3535904.html

 

必須與new共同使用的建構函式名應以大寫字母開頭。當new被省略時JavaScript不會有任何編譯錯誤或運行錯誤拋出。忘記加new時會讓不好的事情發生(比如被當成一般的函數),所以大寫建構函式名是我們來盡量避免這種情況發生的唯一辦法。

javascript變數聲明,hoisting機制,物件導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.