亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript建立物件

JavaScript 提供了一些常用的內(nèi)建物件(稍後介紹),但有些情況下我們需要自訂地建立對象,以達(dá)到特殊的、豐富的功能。

例如我們建立一個(gè)「student」對象,並為其指定幾個(gè)屬性和方法:

student = new Object();  // 創(chuàng)建對象“student”
student.name = "Tom";   // 對象屬性 名字
student.age  = "19";    // 對象屬性 年齡
student.study =function() {   // 對象方法 學(xué)習(xí)
    alert("studying");
};
student.eat =function() {     // 對象方法 吃
    alert("eating");
};

此外,你也可以這樣建立物件:

var student = {};
student.name = "Tom";
……

或這樣:

var student = {
    name:"Tom";
     age:"19";
    ……
}

但是以上方法在建立多個(gè)物件時(shí),會產(chǎn)生大量重複程式碼,所以我們也可以採用函數(shù)的方式新建物件:

function student(name,age) {
    this.name = name;
    this.age = age;
    this.study = function() {
        alert("studying");
    };
    this.eat = function() {
        alert("eating");
    }
}

然後透過new 建立student 物件的實(shí)例:

var student1 = new student('Tom','19');
var student2 = new student('Jack','20');
<!DOCTYPE html>
<html>
<body>
<script>
person={firstname:"Bill",lastname:"gates",age:56,eyecolor:"blue"}
document.write(person.firstname + " is " + person.age + " years old.");
</script>
</body>
</html>


繼續(xù)學(xué)習(xí)
||
<html> <head> <script type="text/javascript"> var person = { name: "dongjc", age: 32, Introduce: function () { alert("My name is " + this.name + ".I'm " + this.age); } }; person.Introduce(); </script> </head> <body> </body> </html>
提交重置程式碼