JavaScript語言精粹 ——筆記

來源:互聯網
上載者:User

1、對象屬性檢索

var student = {     name:"zhan"};

要檢索name,可以用student["name"]  或者 student.name。

2、對象屬性更新

student.name = 'wang';

student.nickname = 'yu';//對象之前沒有nickname屬性,則自動擴充到改對象中。

3、對象的引用

var x = student;

x.nickname = 'Tom';

var nick = student.nickname;

因為x和student都是指向同一個對象的引用,所以nick為'Tom'。

 4、hasOwnProperty 檢查對象是否擁有屬性,它不會檢查原型鏈。

5、用for in 遍曆一個對象中的所有屬性名稱時,改枚舉過程會列出所有屬性,包括函數和原型中的屬性。

  var name;

  for(name in student){

  };

6、4種function調用模式:

  方法調用模式:當一個function被儲存為對象的一個方法,就被稱為方法。

var myObject = {      value:0;      inscrement: function (inc) {         this.value += typeof inc ==='number'? inc  : 1;      }};myObject.increment();document.writeln(myObject.value);  //1myObject.increment(2);document.writeln(myObject.value);  //3

函數調用模式:當function不是一個對象的屬性時,則被當做一個函數來調用

myObject.double = function () {    var that = this;    var helper = function () {        that.value = add(that.value, that.value);    };        helper(); // 以函數的形式調用helper};//以方法的形式調用doublemyObject.double();documnet.writeln(myObject.getValue());  //6

 構造器調用模式

//建立一個名為Quo的構造器函數。它構造一個帶有status屬性的對象。var Quo = function (string) {      this.status = string;};//給Quo的所有執行個體提供一個名為get_status的公用方法。Quo.prototype.get_status = function (){    return this.status;};//構造一個Quo執行個體var myQuo = new Quo("confused");document.writeln(myQuo.get_status());

Apply調用模式:apply方法讓我們構建一個參數數組並用其去調用函數。它允許我們選擇this的值。apply方法接受2個參數。第一個是將被綁定給this的值,第二個就是一個參數數組。

//構造一個包含2個數位數組,並將它們相加var array = [3,4];var sum = add.apply(null,array);  //sum值為7//構造一個包含status成員的對象。var statusObject = {    status:'A-OK'};//statusObject 並沒有繼承自Quo.prototype,但我們可以再statusObject上調用get_status方法,儘管statusObject並沒有一個名為get_status的方法var status = Quo.prototype.get_status.apply(statusObject);//status的值為A-OK

 7、給類型添加方法:

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.