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

JavaScript adds properties and methods to objects

JavaScript can define properties and methods when defining a class, or dynamically add properties and methods after creating an object.

Dynamic addition of properties and methods is difficult to achieve in other object-oriented programming languages ????(C++, JavaScript, etc.), which is a reflection of the flexibility of JavaScript.

Create an object based on the Person class and add properties and methods to it: // Define class

<script>// 定義類
function Person(name,sex) {
    this.name=name;  // 定義一個屬性 name
    this.sex=sex;  // 定義一個屬性 sex
    this.say=function(){  // 定義一個方法 say()
        return "嗨!大家好,我的名字是 " + this.name + " ,性別是 " + this.sex + " 。";
    }
}

// 創(chuàng)建對象
var zhangsan=new Person("張三","男");
zhangsan.say();

// 動態(tài)添加屬性和方法
zhangsan.tel="029-81892332";
zhangsan.run=function(){
    return  " 我跑得很快! ";
}

// 彈出警告框
alert("姓名:"+zhangsan.name);
alert("姓別:"+zhangsan.sex);
alert(zhangsan.say());
alert("電話:"+zhangsan.tel);
alert(zhangsan.run());</script>


Continuing Learning
||
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>無標(biāo)題文檔</title> <script>// 定義類 function Person(name,sex) { this.name=name; // 定義一個屬性 name this.sex=sex; // 定義一個屬性 sex this.say=function(){ // 定義一個方法 say() return "嗨!大家好,我的名字是 " + this.name + " ,性別是 " + this.sex + " 。"; } } // 創(chuàng)建對象 var zhangsan=new Person("張三","男"); zhangsan.say(); // 動態(tài)添加屬性和方法 zhangsan.tel="029-81892332"; zhangsan.run=function(){ return " 我跑得很快! "; } // 彈出警告框 alert("姓名:"+zhangsan.name); alert("姓別:"+zhangsan.sex); alert(zhangsan.say()); alert("電話:"+zhangsan.tel); alert(zhangsan.run());</script> </head> <body> </body> </html>
submitReset Code