Javascript 使用对象(2)- 使用prototype对象扩展对象属性和方法new

http://hi.baidu.com/jrc520/blog/item/cafe07f4c9f04167dcc47470.html

 

JavaScript是基于对象的程序开发语言,在JavaScript中可以创建对象和函数,但创建好了的对象在需要时也可以使用prototype 对象对其属性和方法进行扩展。

1. 定义一个对象
function person(name,age,sex){
this.name=name;
this.age=age;
this.sex=sex;
this.mydisplay=display;
}

2. 对象中的一个方法实现
function display(){
var str="Person: /n";
if(this.name != null)
   str+="name: "+this.name+"/n";
if(this.age != null)
   str+="age: "+this.age+"/n";
if(this.sex != null)
   str+="sex: "+this.sex+"/n";
alert(str);
}

3. 利用prototype对对象的属性 进行扩展
person.prototype.address="BeiJing Road";

4. 利用prototype对对象的方法 进行扩展
person.prototype.showInfo=function(){
var str="Person: /n";
if(this.name != null)
   str+="name: "+this.name+"/n";
if(this.age != null)
   str+="age: "+this.age+"/n";
if(this.sex != null)
   str+="sex: "+this.sex+"/n";
if(this.address != null)
   str+="address: "+this.address+"/n";
alert(str);
}

5. 使用:

var man= new person("Jack",20);
man.mydisplay();
man.showInfo();

6.扩展window方法,不用加prototype

<html>
<head>
<title>无标题文档</title>
<script language="javascript">
<!--
function fullScreen(){
window.moveTo(0,0);
window.outerWidth=screen.width;
window.outerHeight=screen.height;
}

window.maximize=fullScreen;
//-->
</script>
</head>

<body>
<form>
<input type="button" value="click me" onClick="window.maximize();">
</form>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值