JavaScript OOP學習總結

來源:互聯網
上載者:User

標籤:png   原型對象   參數   技術   hello   new   function   on()   自己   

---恢複內容開始---

  • 對象的建立
//用new來建立對象var user = new Object(); //user也是一個對象 user.age = 37;//同時為user對象添加一個年齡的屬性 user.name = ‘周杰倫‘; 

 

//用{}來建立一個對象    var user = {             ‘name‘:‘周杰倫‘,                 ‘age‘:35    }  

 

 

  • 類和建構函式
  //JavaScript中一切都是對象,所有的變數和方法都是可以用來作參數來互相傳遞
function User(name,age){ this.name = name; this.age = age; this.say = function(){ alert(this.name+‘hello word‘); } }

 

 

  • 原型

  1.原型(prototype)

// JavaScript 中,每個方法都有名為“prototype”的屬性,用於引用原型對象。
function User(){ this.name = ‘周杰倫‘; this.age = 37; }
//定義一個User的工作方法
  User.prototype.work = function(){
    alert("我是一個歌手");
}

 

 

  2.原型鏈(__proto__)

    在這裡不寫代碼了,直接

    //這個是網上流傳的

    

 

 

     //這個是自己默寫的(是醜了點,但是都是自己寫出來的,理解了就很容易寫出來)

   

  

---恢複內容結束---

JavaScript OOP學習總結

聯繫我們

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