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

Javascript中對象繼承的實現(xiàn)小例

 更新時間:2014年05月12日 09:47:24   作者:  
這篇文章主要介紹了Javascript中對象繼承的實現(xiàn),需要的朋友可以參考下
復(fù)制代碼 代碼如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
/**
* json對象的格式
{key:value,key:value,key:value..}
*/
//創(chuàng)建對象的小例子
//-----1
var r={};
r.name="tom";
r.age=18;
//-----2
var r={name:"tom",age:20};//json對象
alert(r.age);
//---1,2是等價的
//-------原型模式的寫法
//----1
function Person(){};
Person.prototype.name="中國人";
Person.prototype.age=20;
//原型模式的簡寫形式--2
function Person(){};
Person.prototype={name:"中國人",
age:20,}
//-----1,2等價的
//================================
/* {name:"中國人",
age:20,}
上面的這種格式本身就是個對象,將其付給另一個對象的prototype,就使得
另一個對象的所有屬性。實質(zhì)上就是繼承
*/
//================================
//標準的對象繼承例子,Person,Student
//定義一個Person對象
function Person(){};
Person.prototype.name="中國人";
Person.prototype.age=20;
var person=new Person();
//定義一個Student對象
function Student(){};
Student.prototype=person;
Student.prototype.girlFriend="可以有的";
var stu=new Student();
stu.laop="不許談戀愛";
alert(stu.name);//繼承自父對象的實例
alert(stu.laop);//自己新添加的屬性

//定義一個Teamleader對象的
function Teamleader(){};
Teamleader.prototype=new Student();//繼承自Student
Teamleader.prototype.teamNum=8;//Teamleader自己的屬性
//創(chuàng)建自己的實例
var teamleader=new Teamleader();
alert(teamleader.teamNum);
teamleader.girlFriend="也不可以有哦";
alert(teamleader.name);
//=================================
/*js中繼承的核心就是prototype*/
//=================================
</script>
</head>
<body>

</body>
</html>

相關(guān)文章

最新評論