Jquery 获取对象的另一个方法的值
我正在尝试学习oops的概念,下面我想计算一些东西Jquery 获取对象的另一个方法的值,jquery,Jquery,我正在尝试学习oops的概念,下面我想计算一些东西 (function(){ var rectangle= { specs: function(length, width){ length= length; width= width; this.calculate(); }, calculate: function(){ //How can i refer here the diameter and hei
(function(){
var rectangle= {
specs: function(length, width){
length= length;
width= width;
this.calculate();
},
calculate: function(){
//How can i refer here the diameter and height passed in the specs method above
console.log(length * width)
}
};
rectangle.specs(10, 20);
})();
谢谢根据调用specs方法后希望变量的可访问性,您可以选择两种方法之一 第一种方法是,如果希望变量只包含在列出的方法中,只需将参数传递给正在调用的第二个方法。编辑被**包围:
(function(){
var rectangle= {
specs: function(length, width){
length= length;
width= width;
this.calculate(**length, width**);
},
calculate: function(**length, width**){
//How can i refer here the diameter and height passed in the specs method above
console.log(length * width)
}
};
rectangle.specs(10, 20);
})();
(function(){
var rectangle= {
specs: function(length, width){
**this.length** = length;
**this.width** = width;
this.calculate();
},
calculate: function(){
//How can i refer here the diameter and height passed in the specs method above
console.log(**this.length * this.width**)
}
};
rectangle.specs(10, 20);
})();
第二种方法将变量分配给对象,因此可以使用rectangle.length或rectangle.width在对象外部访问变量,或者在内部使用“this”关键字访问变量。编辑被**包围:
(function(){
var rectangle= {
specs: function(length, width){
length= length;
width= width;
this.calculate(**length, width**);
},
calculate: function(**length, width**){
//How can i refer here the diameter and height passed in the specs method above
console.log(length * width)
}
};
rectangle.specs(10, 20);
})();
(function(){
var rectangle= {
specs: function(length, width){
**this.length** = length;
**this.width** = width;
this.calculate();
},
calculate: function(){
//How can i refer here the diameter and height passed in the specs method above
console.log(**this.length * this.width**)
}
};
rectangle.specs(10, 20);
})();
您可以使用
this
keywordCool。谢谢,艾基