prototype主要用来提供对象所属于类的功能,可以利用prototype来扩展类的功能
例如:
WebFXTreeItem.prototype.getLast = function() {
// codes that return the tree's last node here
}
定义WebFXTreeItem的getLast 函数,在JS中就可以使用WebFXTreeItem.getLast()
例如:
WebFXTreeItem.prototype.getLast = function() {
// codes that return the tree's last node here
}
定义WebFXTreeItem的getLast 函数,在JS中就可以使用WebFXTreeItem.getLast()
<script language=Javascript> //自己动手为string添加Trim
String.prototype.trim = function(){return this.replace(/(^\s+)|(\s+$)/g,"");}
var str = " meizz ";
alert(str.trim());
</script>
function a(){
this.name = "a.name";
this.sex = "male";
}
function b(){
this.age = 27;
}
b.prototype = new a();
var bb = new b();
alert(bb.name); //结果应为 a.name
</script>
http://www.corsak.com
另外, 有做简单的应用出来, 能分享一下吗?
中的基本语法
String.prototype.trim = function(){return this.replace(/(^\s+)|(\s+$)/g,"");}
老大给的例子
就类似添加一种类的方法
http://birdshome.cnblogs.com/archive/2005/02/17/105403.html
function array_max( ){
var i, max = this[0];
for (i = 1; i < this.length; i++)
{
if (max < this[i])
max = this[i];
}
return max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max();
alert(y);
</script>
里面封装好了AJAX等方面的应用。