开发者

Calling an object method from an object property definition [duplicate]

开发者 https://www.devze.com 2023-02-02 05:05 出处:网络
This question already has answers here: Self-references in object literals / initializers (30 answers) Closed 10 months ago.
This question already has answers here: Self-references in object literals / initializers (30 answers) Closed 10 months ago.

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 n开发者_运维问答otation variants of all those as well, ex. this["method"], with no luck.


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.


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();
   }
};


/* 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();
 */


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'.


It worked for me as follows:

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


Wouldn't you just go:

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

精彩评论

暂无评论...
验证码 换一张
取 消