js之prototype 原型对象

原型对象prototype可以这么理解,是该类的实例对象的模板,每个实例对象都是先复制一份该类的prototype,通过这个可以让类的实例拥有相同的功能
 
String.prototype.say=function(){
    alert(this);
};
"test".say();
这样就给String的实例对象添加了一个方法,当然也可以添加其他的属性、方法。
通过prototype可以让js模拟面向对象的功能
其他类型
Array.prototype.unset = function(string, limit) {}
Date.prototype.format= function(string) {}
Fnc.prototype.xxx = function(xxx,xx){} //fnc是你的js函数名

猜你喜欢

转载自www.cnblogs.com/rdchen/p/10784249.html
今日推荐