欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

創(chuàng)建js對象和js類的方法匯總

 更新時(shí)間:2014年12月24日 11:14:41   投稿:hebedich  
這篇文章主要介紹了2種創(chuàng)建js對象和js類的方法,十分的簡單,推薦給大家。

代碼很簡單,就不多廢話了。

復(fù)制代碼 代碼如下:

//第一種定義方式
var person=new Object(); //創(chuàng)建了一個(gè)對象.
person.name="tom"; //使用person對象對調(diào)用name屬性,它的值為tom
alert(person.name); //顯示name屬性值
person.say=function(){ //對person對象添加了一個(gè)say函數(shù)。
alert("person say");
};
person.say();

復(fù)制代碼 代碼如下:

//第二種定義方式
var person={
name:"tom",
say:function(){
alert("hello person");
}
}; //創(chuàng)建了一個(gè)對象.
//alert(person.name);
//person.say();
person.age=10;
alert(person.age);
//js中定義類是使用function。
var Person = function(name){ //我們在定義一個(gè)類。 相當(dāng)于它具有一個(gè)有參數(shù)的構(gòu)造函數(shù)。
this.name =name;//類的屬性
this.say = function(){ //類的方法.
alert("say good");
}
}
var p = new Person("fox"); //定義Person類的一個(gè)對象p
alert(p.name); //調(diào)用name屬性

相關(guān)文章

最新評論