JavaScript建立對象的方式

來源:互聯網
上載者:User

標籤:obj   var   定義   asc   一個   ring   post   就是   建立   

方法1:

var 對象名=new object();先建立一個Null 物件

對象名.屬性名稱=值; 給對象添加一個屬性

對象名.屬性名稱=function(){}給對想添加一個方法

var box=new boject();

box.name="李";

box.age=18; 數字不用加引號

box.run=function(){

box.name+box.age

}

使用的方法:

對象.屬性名稱

方法2:

{}花括弧形式的自訂對象
花括弧中,多組屬性,我們必須使用,逗號進行分隔。
var 對象名 = {
屬性名稱: 值, // 給對象添加屬性
屬性名稱(函數名): function(){} // 給對象添加方法
}

使用的方法:

對象.屬性名稱

var box{

name:"uer",

age:12,

run:function(){}

}

使用的方法就是,box.name

方法3

function形式的自訂對象(在js中函數即是對象)
function 對象名(){
this.屬性名稱 = 值; // 給對象添加屬性
this.函數名 = function(){} //給對象添加方法
}

使用方法

 

 

var 變數名 = new 對象名() // 建立一個對象的執行個體     變數名.屬性名稱

function box(){
this.name="user";
this.age=30;

}
使用方法
var box2= new box();


 

JavaScript建立對象的方式

聯繫我們

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