对象的创建、使用及向String对象添加方法(prototype的使用)

prototype 属性使您有能力向对象添加属性和方法。

语法


object.prototype.name=value


<html>
<head>
<script language="javascript" type="text/javascript">
	//对象的定义,使用(名片示例)
	function Card(name, address, work, home){
		this.name = name;
		this.address = address;
		this.work = work;
		this.home = home;
		this.show = ShowCard;
	}
	function ShowCard(){
		l1 = "name:" + this.name + ".<br/>";
		l2 = "address" + this.address + ".<br/>";
		l3 = "work" + this.work + ".<br/>";
		l4 = "home" + this.home + ".<br/>";
		document.write(l1,l2,l3,l4);
	}
	var tom = new Card("tom","河口","程序员","北京");
	tom.show();
	
	//向String对象添加一个heading方法。将字符串显示为标题,然后用这个方法显示3个标题
	function addhead(level){
		html = "H" + level;
		text = this.toString();
		a = "<" + html + ">";
		b = "<" + html + "/>";
		return a + text + b;
	}
	String.prototype.heading = addhead;
	document.write("this is h1".heading(1)+"</br>");
	document.write("this is a h3".heading(3)+"</br>");
	document.write("this is a h5".heading(5)+"</br>")
</script>
</head>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值