[b]第一种方法:[/b]
[i]var obj={};[/i]
example:
var so = { "name":"what?",
"action":"study & think",
"doo": function(){
console.log(this.name+" I am doing "+this.action);
}
};
so.doo(); //output: what? I am doing study & thinking
//add new function to the class
so.dee=function(){
console.log(this.name+"hwllo: I am stronger " + this.action);
}
so.dee(); //output: what? hwllo: I am stronger study & think
也可以
var so = new Object();
so = { ...} //同上
用此方式实现 Stack 类
var Stack = { "idx":0,
"store": new Array(200),
"push": function(comp){
this.store[this.idx]=comp;
this.idx++;
},
"pop": function(){
var re=this.store[this.idx-1];
this.idx--;
return re;
},
"isEmpty":function(){
if(this.idx==0) return true;
return false;
}
};
Stack.push("aaaa");
Stack.push("</definition>\n");
这个必须直接调用,不能用 new Stack(),来定义定义实例。
// 第二种方法
就好像 java 里的类与实例 ,不可以引用类(myclass)的值 ,只可以引用实例 (st1) 的值, prototype.xx 就好像是Java 里的 public ,var xxx 就是父类的私有变量, this.xxx 就是父变量,可以传递给实例
var myclass = function(){
[color=red]this[/color].name="what"; //必须用this,假如想调用初始变量值的话
var action="who do who";
var firstAction=function(){
console.log("inner class"+name);
}
}
myclass.prototype.doAction = function(name){
console.log(name+"==:)");
}
myclass.prototype.name="what name";
var st1 = new myclass();
console.log(myclass.name); //output: undefined ,
console.log(st1.name); // output: what
st1.doAction(33); // output: 33=:)
如果 已经在类中定义 this.xxx ,后面又用 prototype.xxx 定义不会生效
如 var myclass =function(){
this. name="good";
}
myclass.prototype.name="what";
var st1=new myclass();
console.log(st1.name); // good
所以定义类,要么定义一个空function(), 然后用 prototype.xx 增加属性和函数
要么全部用 this.xxx 定义, 这样就不会产生冲突
[i]var obj={};[/i]
example:
var so = { "name":"what?",
"action":"study & think",
"doo": function(){
console.log(this.name+" I am doing "+this.action);
}
};
so.doo(); //output: what? I am doing study & thinking
//add new function to the class
so.dee=function(){
console.log(this.name+"hwllo: I am stronger " + this.action);
}
so.dee(); //output: what? hwllo: I am stronger study & think
也可以
var so = new Object();
so = { ...} //同上
用此方式实现 Stack 类
var Stack = { "idx":0,
"store": new Array(200),
"push": function(comp){
this.store[this.idx]=comp;
this.idx++;
},
"pop": function(){
var re=this.store[this.idx-1];
this.idx--;
return re;
},
"isEmpty":function(){
if(this.idx==0) return true;
return false;
}
};
Stack.push("aaaa");
Stack.push("</definition>\n");
这个必须直接调用,不能用 new Stack(),来定义定义实例。
// 第二种方法
就好像 java 里的类与实例 ,不可以引用类(myclass)的值 ,只可以引用实例 (st1) 的值, prototype.xx 就好像是Java 里的 public ,var xxx 就是父类的私有变量, this.xxx 就是父变量,可以传递给实例
var myclass = function(){
[color=red]this[/color].name="what"; //必须用this,假如想调用初始变量值的话
var action="who do who";
var firstAction=function(){
console.log("inner class"+name);
}
}
myclass.prototype.doAction = function(name){
console.log(name+"==:)");
}
myclass.prototype.name="what name";
var st1 = new myclass();
console.log(myclass.name); //output: undefined ,
console.log(st1.name); // output: what
st1.doAction(33); // output: 33=:)
如果 已经在类中定义 this.xxx ,后面又用 prototype.xxx 定义不会生效
如 var myclass =function(){
this. name="good";
}
myclass.prototype.name="what";
var st1=new myclass();
console.log(st1.name); // good
所以定义类,要么定义一个空function(), 然后用 prototype.xx 增加属性和函数
要么全部用 this.xxx 定义, 这样就不会产生冲突