扩展内置对象

JavaScript 提供了扩展内置对象定义的功能。
使用prototype关键字,可以向内置对象添加属性和方法。

代码如下


<html>
	<body>
		<script language="javascript">
			function addhead(str){
				html = "H" + str;
				text = this.toString();
				start = "<" + html + ">";
				stop = "</" + html + ">";
				return start + text + stop;
			}
			String.prototype.heading = addhead;
			String.prototype.attr = "Attribute";
			document.write("This id a heading 1".heading(1));
			document.write("This id a heading 2".heading(2));
			document.write("This id a heading 3".heading(3));
			document.write(new String().attr);
		</script>
	</body>
</html>

页面效果如下

原文地址:https://www.cnblogs.com/jxgapyw/p/5093925.html