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

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

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

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

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

</body>
</html>

相關(guān)文章

最新評(píng)論