8

I am trying to call an object method from an object (the same object) property definition to no avail.

var objectName = {
     method   :   function() {
          return "boop";
     },
     property :   this.method()
};

In this example I want to assign the return value of objectName.method ("boop") to objectName.property.

I have tried objectName.method(), method(), window.objectName.method(), along with the bracket notation variants of all those as well, ex. this["method"], with no luck.

Ian
  • 83
  • 1
  • 1
  • 3
  • I could change 'property' to a method: method2 : function() { return this.method(); } but I wish to avoid that as the contents will not be dynamic. – Ian Jan 01 '11 at 23:36
  • There are no such thing as dynamic properties in any current version of JavaScript.. – PatrikAkerstrand Jan 02 '11 at 00:01
  • What I meant is that the value of the property will not need to change after the initialization. – Ian Jan 02 '11 at 00:04

6 Answers6

9

At initialization this does not refer to the object holding the property method (which is not yet initialized) but to the curent context - and since this has no method property you will get a TypeError.

If it is a custom getter you want, then you might look into using getters and setters in javascript - they are not supported by ECMAscript prior to ES5, but many engines support them nonetheless.

Sean Kinsey
  • 37,689
  • 7
  • 52
  • 71
1

I can see no reason why you would want to do this?

Why not just use a getter, if you don't want to use the method name.

var objectName = {
   method   :   function() {
        return "boop";
   },
   property :   function () {
        return this.method();
   }
};
Mads Mogenshøj
  • 2,063
  • 1
  • 16
  • 23
  • I think it does not solve the question. You still need to call objectName.property() as a function, you can't do objectName.property to obtain the value itself. – Guido Dec 17 '13 at 08:47
0

One more way of doing this:

var objectName = {
    method : function() {
        return "boop";
    }
};

$.extend(objectName, {  
    property : objectName.method()
})

objectName already initialized at the time of calling 'method'.

boades
  • 395
  • 1
  • 11
0

It worked for me as follows:

var objectName = {
     method   :   function() {
          return "boop";
     },
     property :   objectName.method()
};
thanks_in_advance
  • 2,603
  • 6
  • 28
  • 44
0
/* overwrites the `property` function with a the set value
 * the first time it's called.  From then on it's just a 
 * property
 */
var objectName = {
    method: function(){ return 'boo'; },
    property: function(){
        var returnValue = this.method();
        this.property = returnValue;
        return returnValue;
    }
};

/* or */
var objectName = {
    property: ( function(){ return 'boo'; }() );
};
/* this evaluates the anonymous function within 
 * the parenthetical BEFORE the definition of 
 * objectName leaving only the returned value 
 * to be set to property
 */

/* or */

var objectName = {
    method: function(){
        this.property = 'boop';
        return this.property;
    }
}
/* calling the method to create/set the property
 * for objectName to the evaluated value
 */

/* or */

var objectName = {
    data: {
        property: 'boop'
    },
    property: function( value ){
        if ( value ) this.data.property = value;
        return this.data.property;
    }
};

/* the last one, I believe, is a neat way of handling 
 * both set and get stealing the concept from my use 
 * with JQuery.
 * set = objectName.property( 'boo' );
 * get = objectName.property();
 */
-1

Wouldn't you just go:

var objectName = {
     method   :   function() { return "boop"; },
     property :   function() { return this.method(); }
};
mikesigs
  • 10,491
  • 3
  • 33
  • 40