Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 获取对象的另一个方法的值_Jquery - Fatal编程技术网

Jquery 获取对象的另一个方法的值

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

我正在尝试学习oops的概念,下面我想计算一些东西

(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。谢谢,艾基