<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>構造對象</title>
<script type="text/javascript">
/*構造函數其實就是一個普通的函數,只有後面加new的時候,纔會作爲函數原型和new的實例關聯起來,js裏面只有實例,沒有類的概念*/
function student(name,age){
this.name=name;
this.age=age;
this.run=function(){
alert(this.name+"在跑");
}
}
//通過原型的prototype方法可以指向所有student對象,讓所有這些實例共享hello方法
student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
};
var c=new student("zs",20);
console.log(c.name);
console.log(c.age);
c.run();
c.hello();
</script>
</head>
<body>
</body>
</html>
js構造函數和對象
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.