javascript 如何以对象字面量表示法创建方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17486854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create a method in object literal notation?
提问by Vato
I have started web-programming for only 13 days on Codeacademy.comand find it a good place to learn. But I still sometimes misunderstand things. I always check them on w3school.com,but this time I couldn't find the information that I needed. So as I learned there are 2 types of creating objects first: object literal notation and second: Object constructor. I have learned that there are also methods and functions, but I couldn't understand how to create a method in object literal notation? In object constructor I just write : `
我在Codeacademy.com上开始网络编程仅 13 天,并发现它是一个学习的好地方。但我有时还是会误解一些事情。我总是在w3school.com上查看它们,但这次我找不到我需要的信息。因此,据我所知,创建对象有两种类型:对象文字表示法和第二个:对象构造函数。我了解到也有方法和函数,但我不明白如何用对象文字表示法创建方法?在对象构造函数中,我只写:`
var bob = new Object();
bob.age = 30;
bob.setAge = function (newAge)
{
bob.age = newAge;
};
Can you please tell me how to do the same when writing object literal notation.
你能告诉我在编写对象文字符号时如何做同样的事情吗?
var bob = {
age: 30
};
回答by Denys Séguret
Syntactically, the change is very simple :
从语法上讲,更改非常简单:
var bob = {
age: 30,
setAge: function (newAge) {
bob.age = newAge;
}
};
But as you can see, there's a problem : as in your code it uses the external bob
variable so this wouldn't work if you change the value of the bob
variable.
但是正如您所看到的,存在一个问题:因为在您的代码中它使用外部bob
变量,因此如果您更改bob
变量的值,这将不起作用。
You can fix that with
你可以用
var bob = {
age: 30,
setAge: function (newAge) {
this.age = newAge;
}
};
Note that at this point you should check whether what you need isn't, in fact, a class, which would bring some performance improvements if you have several instances.
请注意,此时您应该检查您需要的是否实际上不是class,如果您有多个实例,它会带来一些性能改进。
Update:ECMAScript 6 now allows methods to be defined the same way regardless of whether they are in an object literal:
更新:ECMAScript 6 现在允许以相同的方式定义方法,无论它们是否在对象字面量中:
var bob = {
age: 30,
setAge (newAge) {
this.age = newAge;
}
};
回答by jAndy
Its nothing different and as easy as
它没有什么不同,就像
var bob = {
age: 30,
setAge: function( newAge ) {
this.age = newAge;
}
};
Alternatively, you can create a real setterfunction either by invoking Object.defineProperty()
or as simple as
或者,您可以通过调用或简单地创建一个真正的setter函数Object.defineProperty()
var bob = {
age: 30,
firstName: 'j',
lastName: 'Andy',
set setName( newName ) {
var spl = newName.split( /\s+/ );
this.firstName = spl[ 0 ];
this.lastName = spl[ 1 ];
}
}
Where you could go like
你可以去哪里
bob.setName = "Thomas Cook"; // which sets firstName to "Thomas" and lastName to "Cook"
回答by Fafoon
The last code you posted is missing a comma. Also, you don't need a ';' after a function definition of an object's property. Like this:
您发布的最后一个代码缺少一个逗号。此外,您不需要';' 在对象属性的函数定义之后。像这样:
var object2 = {
name: "Fred",
age: 28,
club: "Fluminense",
bio2: function (){
console.log(this.name +" is "+ this.age + " years old and he is playing in "+ this.club);
}
};
回答by mikefirsov
This is the way to solve this exercise using literal object creation method:
这是使用文字对象创建方法解决此练习的方法:
var setAge = function (newAge) {
this.age = newAge;
};
var bob = new Object();
bob.age = 30;
bob.setAge = setAge;
var susan = {
age: 25,
setAge: setAge
}
susan.setAge(35);
回答by Razan Paul
If you want encapsulation, you might use the following syntax(self-executing function). Here age is not accessible from the outside of the object bob.
如果你想要封装,你可以使用下面的语法(自执行函数)。这里的年龄不能从对象 bob 的外部访问。
var bob = (function() {
//...private
var age = 30;
function setAge(newAge) {
age = newAge;
};
function getAge() {
return age;
}
// Public api
return {
setAge: setAge,
getAge: getAge
}
}());
bob.setAge(50);
alert(bob.getAge());
jsfiddle: http://jsfiddle.net/61o9k98h/1/
jsfiddle:http: //jsfiddle.net/61o9k98h/1/
回答by Neeraj Singh
Like this:
像这样:
var bob = {
age: 30,
setAge: function (age) {
this.age = age;
}
}
alert(bob.age); // 30
bob.setAge(45); // set age = 45
alert(bob.age); // 45