国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

JavaScript建立物件

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

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

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";
    ……
}

但是以上方法在建立多個物件時,會產(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 物件的實例:

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>
提交重置程式碼