如何在JS [duplicate]中引用当前对象的属性

时间:2022-08-27 18:13:06

Possible Duplicate:
Self-references in object literal declarations

可能的重复:对象文字声明中的自引用

I have some simple objects in JS like this example:

我有一些JS中的简单对象,比如这个例子:

var object = {
 firstname : 'john',
 lastname : 'paul',
 wholename : firstname + lastname
}

Well this simple thing doesn't work; john and paul are undefined in wholename, so I tried to use the 'this' operator which works ONLY if I do a function (getWholeName(){return this.firstname+this.lastname} ). But if I want to use a variable and not a function, how can I do? I also tried object.firstname + object.lastname but it doesn't work.

这个简单的东西不管用;john和paul在wholename中没有定义,所以我尝试使用“this”操作符,它只在执行函数(getWholeName(){返回this.firstname+this。lastname })。但是如果我想用一个变量而不是一个函数,我该怎么做呢?我也试过对象。firstname +对象。姓,但不行。

2 个解决方案

#1


12  

There is no way to reference the object, but you can add the properties dynamically:

无法引用该对象,但是可以动态地添加属性:

var object = { 
    firstname : 'john', 
    lastname : 'paul'
};

object.wholename = object.firstname + object.lastname;

EDIT:

编辑:

And why not wrap it in a function?

为什么不把它包起来呢?

var makePerson = function (firstname, lastname) {
    return {
        firstname: firstname,
        lastname: lastname,
        wholename: firstname + lastname  // refers to the parameters
    };
};

var object = makePerson('john', 'paul');

#2


2  

In Javascript, every function is an object. You should declare your Object's constructor as a function like this:

在Javascript中,每个函数都是一个对象。您应该将对象的构造函数声明为如下函数:

function person(firstname,lastname)
{
this.firstname=firstname;
this.lastname=lastname;

this.wholeName=wholeName;

 //this will work but is not recommended.
 function wholeName()
 {
 return this.firstname+this.lastname;
 }
}

you can add extra methods to your object by prototyping it aswell , which is the recommended way of doing things. More info here:

您还可以通过原型化对象向对象添加额外的方法,这是推荐的方法。更多信息:

http://www.javascriptkit.com/javatutors/proto.shtml

http://www.javascriptkit.com/javatutors/proto.shtml

#1


12  

There is no way to reference the object, but you can add the properties dynamically:

无法引用该对象,但是可以动态地添加属性:

var object = { 
    firstname : 'john', 
    lastname : 'paul'
};

object.wholename = object.firstname + object.lastname;

EDIT:

编辑:

And why not wrap it in a function?

为什么不把它包起来呢?

var makePerson = function (firstname, lastname) {
    return {
        firstname: firstname,
        lastname: lastname,
        wholename: firstname + lastname  // refers to the parameters
    };
};

var object = makePerson('john', 'paul');

#2


2  

In Javascript, every function is an object. You should declare your Object's constructor as a function like this:

在Javascript中,每个函数都是一个对象。您应该将对象的构造函数声明为如下函数:

function person(firstname,lastname)
{
this.firstname=firstname;
this.lastname=lastname;

this.wholeName=wholeName;

 //this will work but is not recommended.
 function wholeName()
 {
 return this.firstname+this.lastname;
 }
}

you can add extra methods to your object by prototyping it aswell , which is the recommended way of doing things. More info here:

您还可以通过原型化对象向对象添加额外的方法,这是推荐的方法。更多信息:

http://www.javascriptkit.com/javatutors/proto.shtml

http://www.javascriptkit.com/javatutors/proto.shtml