function Obj(){
var a = "a";
var b = "b";
var c = "c";
function getA(){
return a;
}
function getB(){
return b;
}
function getC(){
return c;
}
function setA(a){
thsi.a = a;
}
function getB(b){
this.b = b;
}
function getC(c){
this.c = c;
}
}var o = new Obj();
document.write("o="+o);
HTML 输出结果: o=[object Object]document.write("o="+o.getA()); //报错!我要如何能得到Obj这个函数里面的方法???
你可以通过定义特权函数:
this.getA = function(){return a};
也可以在Obj类的外部给Obj原型添加getA 的方法:
Obj.prototype.getA = function(){return a};
这两种方法通过Obj的实例调用getA方法
this.a=a;
}
obj.prototype.getA = function(){
alert(this.a);
}
var o = new obj("hello");
o.getA();
this.a = "a";
this.b = "b";
this.c = "c";
function getA(){
return this.a;
}
function getB(){
return this.b;
}
function getC(){
return this.c;
}
function setA(a){
thsi.a = a;
}
function getB(b){
this.b = b;
}
function getC(c){
this.c = c;
}}
function Obj(){
this.a = "";
this.b = "";
this.c = "";
}
Obj.prototype.setA = function(str){
this.a = str;
}
Obj.prototype.getA = function(){
return this.a;
}
Obj.prototype.getB = function(){
return this.b;
}
Obj.prototype.getC = function(){
return this.c;
}
var obj = new Obj()
obj.setA("test");
alert(obj.getA())
this.a = "";
this.b = "";
this.c = "";
}
Obj.prototype.setA = function(str){
this.a = str;
}
Obj.prototype.getA = function(){
return this.a;
}
Obj.prototype.getB = function(){
return this.b;
}
Obj.prototype.getC = function(){
return this.c;
}
var obj = new Obj()
obj.setA("test");
alert(obj.getA())
up
居然把用java的方法用在Javascript上
var a = "a";
var b = "b";
var c = "c";
function getA(){
return a;
}
this.GetA=getA();
function getB(){
return b;
}
function getC(){
return c;
}
function setA(a){
this.a = a;
}
function getB(b){
this.b = b;
}
function getC(c){
this.c = c;
}}var o = new Obj();
console.log("o="+o.GetA);
var a = "a";
var b = "b";
var c = "c";
this.getA = function(){
return a;
}
this.getB = function(){
return b;
}
this.getC = function(){
return c;
}
function setA(val){
a = val;
}
function getB(val){
b = val;
}
function getC(val){
c = val;
}
}
var o = new Obj();
document.write("o="+o.getA()); 这是你要的效果吧,a,b,c只能通过set来传值
var a = "a";
var b = "b";
var c = "c";
this.getA = function(){
return a;
}
this.getB = function(){
return b;
}
this.getC = function(){
return c;
}
this.setA = function(val){
a = val;
}
this.setB = function(val){
b = val;
}
this.setC = function(val){
c = val;
}
}
var o = new Obj();
document.write("o="+o.getA()); 这是你要的效果吧,a,b,c只能通过set来传值,上面的写错了