当前位置:首页 > H5 > 正文内容

javascript使用原型为内置对象增加方法

高老师9年前 (2017-10-07)H52140
//字面量的形式创建原型对象
/*
function Box(){}
Box.prototype={
	'name':'gao',
	age:23,
	fun:function(){
		return this.name+'--'+this.age;
	}
}

var box1=new Box();
console.log(box1.age)
console.log(box1.constructor)
*/

//1.使用字面量后constructor会指向Object,因为Box.prototype={}这种写法创建了一个新的对象导致.
//解决方式如下:
function Box(){}
Box.prototype={
	constructor:Box,
	'name':'gao',
	age:23,
	fun:function(){
		return this.name+'--'+this.age;
	}
}

var box1=new Box();
console.log(box1.age)
console.log(box1.constructor)

//2.原型对象不仅仅在自定义对象中可以使用,内置的引用类型都可以使用,并且本身页使用了原型

var box2=[1,15,9,8,6,12];
console.log(Array.prototype.sort());

//3.给原型增加方法
String.prototype.addstring=function(){
	return this+'-Hello!';
}

console.log('China'.addstring());

扫描二维码推送至手机访问。

版权声明:本文由高久峰个人博客发布,如需转载请注明出处。

本文链接:https://blog.20230611.cn/post/261.html

分享给朋友:

“javascript使用原型为内置对象增加方法” 的相关文章

Javascript事件冒泡和捕捉

Javascript事件冒泡和捕捉

捕捉模式从DOM最顶层一直到最后一层,冒泡正好相反,具体运行以下实例测试.<!DOCTYPE html> <html> <head> <title>捕捉和冒泡</title> <meta charset="...

Javascript事件冒泡和捕捉的阻止

Javascript事件冒泡和捕捉的阻止

<!DOCTYPE html> <html> <head> <title>捕捉和冒泡</title> <meta charset="utf-8"> </head> <bo...

Javascript获取Mac地址

Javascript获取Mac地址

<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>获取mac地址</title> </head&g...

 js图片旋转插件jqueryrotate

js图片旋转插件jqueryrotate

jQuery 插件——jqueryrotate,它可以实现旋转效果。jqueryrotate 支持所有主流浏览器,包括 IE6。如果提示方法不存在,可能是你的Jquery版本过低或者过高。基本语法:$('#img').rotate(90);//旋转90度其他的参数:参数类型说明默认值...

 js 工厂模式示例,js工厂模式原理

js 工厂模式示例,js工厂模式原理

查看文章前你需要了解以下2点://1.this指向的是windows对象,通过console.log(this)可以查看到对象包含所有的方法和属性//2.全局变量属于this对象的属性通过console.log(this)可以查看到对象包含我们设置的全局变量我们经常在创建相同结构的Js对象会重复的设...

js 点击复制,js实现手机端点击复制,js实现点击复制,js点击复制到剪贴板

js 点击复制,js实现手机端点击复制,js实现点击复制,js点击复制到剪贴板

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>点击复制</title> </head>...