0

if i have a function as a property of an object, do function closure rules still apply? i remember reading that a function is an object, but i also understand that an object is not a function.

More specifically can i grab and edit the other properties inside the same object without referencing the object in that function? Here's an example:

someObj = {
property : 44,
calculate : function(){
    property * moreproperties;
};

or do i do this?

someObj = {
property : 44,
calculate : function(){
   someObj.property * someObj.moreproperties;
};
Andrew
  • 737
  • 2
  • 8
  • 24
  • @GOTO0—that only works if *this* within the function is set to *someObj* (e.g. it's called as `someObj.calculate()`, but not for `var x = someObj.calculate; x()`. – RobG Jan 07 '16 at 00:10
  • The duplicate question isn't identical, but the answers suit. – RobG Jan 07 '16 at 00:20

1 Answers1

1

You can do it using this keyword

someObj = {
property : 44,
calculate : function(){
   this.property * this.moreproperties;
};

if you have another function like callback for an event in jQuery store this into variable

someObj = {
property : 44,
calculate : function(){
   var parent = this;
   $('#some-element').click(function() {
       parent.something * parent.somethingElse
   });
};
Covik
  • 746
  • 6
  • 15