javascript class 定义
Method 1:
var TestClass=function(){
this.fun1=function(){return "我是中国人";};
this.field1="这是字段";
this.i1=10;
this.i2=20;
this.sum=function(){return this.i1+this.i2; };
};
var tc=new TestClass();
alert(tc.fun1() );
alert(tc.field1 );
Method 2:
//创建连接一个字符串父类
function StringBuffer()
{
//私有属性 _strings
this._strings= new Array;
}
//以下为StringBuffer类2个方法
StringBuffer.prototype.append = function (str)
{
this._strings.push(str);
}
StringBuffer.prototype.toString = function(){
return this._strings.join('');
}
var TestClass=function(){
this.fun1=function(){return "我是中国人";};
this.field1="这是字段";
this.i1=10;
this.i2=20;
this.sum=function(){return this.i1+this.i2; };
};
var tc=new TestClass();
alert(tc.fun1() );
alert(tc.field1 );
Method 2:
//创建连接一个字符串父类
function StringBuffer()
{
//私有属性 _strings
this._strings= new Array;
}
//以下为StringBuffer类2个方法
StringBuffer.prototype.append = function (str)
{
this._strings.push(str);
}
StringBuffer.prototype.toString = function(){
return this._strings.join('');
}
<script type="text/javascript">
var TestClass=function(){
this.fun1=function(){return "我是中国人";};
this.field1="这是字段";
this.i1=10;
this.i2=20;
this.sum=function(){return this.i1+this.i2; };
};
TestClass.prototype.toString=function(){
return "I1:"+this.i1;
};
var v1=new TestClass();
v1.i1=1;
var v2=new TestClass();
v2.i1=3;
var v3=new TestClass();
v3.i1=2;
function SortDemo(){
var a, l; // 声明变量。
//a = new Array(1,2,10,9,8,7);
a=[];
a.push(v1);
a.push(v2);
a.push(v3);
l = a.sort(cmp); // 排序数组。
alert(l ); // 返回排序的数组。
}
SortDemo();
function cmp(a,b){
return -a.i1+b.i1;
//return -(a-b);
//return parseInt(a)-parseInt(b);
}
</script>