简体   繁体   English

在子参数方法中如何使用此关键字到达元素?

[英]How to reach element with this keyword in children parameter methods?

I have an object like this 我有一个这样的物体

function myObject(){
    this.$domElement = $('#apple');
    this.name = 'apple';
    this.color = 'green';
    this.price = '200';

    this.show = function(){
       this.$domElement.show()
    }
}

Now I want to add a new method to this object like this 现在我想像这样向该对象添加一个新方法

this.setCss = {
    position : function(){
        //...
    },

    opacity : function(){
        //...
    },

    border : function(){
        //...
    }
}

Now, my question : How can I reach my object again with this keyword. 现在,我的问题是:如何使用this关键字再次到达我的对象。

The final code I am expecting is something like this. 我期望的最终代码是这样的。 If possible... 如果可能的话...

function myObject(){
    this.$domElement = $('#apple');
    this.name = 'apple';
    this.color = 'green';
    this.price = '200';
    this.show = function(){
       this.$domElement.show()
    }
    this.setCss = {
        position : function(){
        //...this.$domElement.css({top:10,left:20})
        },
        opacity : function(){
        //...this.$domElement.css({opacity:.5})
        },
        border : function(){
        //...this.$domElement.css({border:'1px solid red'})
        }
    }
}

try this 尝试这个

 function myObject(){
   //init
   var me = this;

    this.$domElement = $('#apple');
    this.name = 'apple';
    this.color = 'green';
    this.price = '200';

    this.show = function(){
       //use
       me.$domElement.show()
    }
}

var obj = new myObject();
obj.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM