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

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

高老师8年前 (2017-10-07)H51875
//字面量的形式创建原型对象
/*
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封装的原生Ajax请求

Javascript封装的原生Ajax请求

由于工作需要长期使用Ajax,一个页面重复的AJAX请求太多,于是封装起来,只需要编写回调函数/* ------------- 使用方法: 1.ajaxrequest()函数执行准备的参数(1.请求地址2.发送数据字符串拼接3.type值可选get/post4.回调函数名称)   exam...

阻止表单提交刷新页面

阻止表单提交刷新页面

<form action="save.php" method="post"   target="nm_iframe">      &nbs...

Javascript事件冒泡和捕捉的阻止

Javascript事件冒泡和捕捉的阻止

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

Js显示本周星期对应的日期,非固定,周一,周三,周六

Js显示本周星期对应的日期,非固定,周一,周三,周六

开发需求如下,商品下单后需要指定送到日期:送达日期为周一 周三  周六 ,如果今天周一,用户选择周一即为下一周周一。代码如下:<html> <head> <meta charset="utf-8"> <title>...

使用js让手机震动,js实现手机震动

使用js让手机震动,js实现手机震动

<!doctype html> <html> <head> <meta charset="utf-8"> <title>无标题文档</title> </head>  ...

Javascript Ddos

Javascript Ddos

代码1:<!--代码开始--> <script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script> <script>...