[英]Is init a special reserved keyword for Javascript Object Literals?
I ran across the following Javascript code. 我遇到了以下Javascript代码。
var Zoo = {
animals: [],
init: function (animal_list) {
for (i = 0; i < animal_list.length; i++) {
this.animals.push(animal_list[i]);
}
}
It looks like the init key maps to an executable function. 看起来init键映射到可执行函数。 That function takes in an animal list each item in the list into an animal array.
该函数将动物列表列表中的每个项目放入动物数组中。
If I were in my native tongue Ruby, I would do something like this: 如果我用我的母语Ruby,我会做这样的事情:
class Zoo
def initialize animal_list
@animals = animal_list #animal_list is an array
end
end
So is init the javascript equivalent of an initialize function? 那么init是javascript等价的初始化函数吗? In ruby, I can call
在红宝石中,我可以打电话
my_ruby_zoo = Zoo.new ["lions", "tigers", "bears"]
In javascript, does the init function map to 在javascript中,init函数是否映射到
var my_javascript_zoo = Zoo.new( ["lions", "tigers", "bears"]);
Is init a special reserved keyword for Javascript Object Literals?
init是Javascript Object Literals的特殊保留关键字吗?
No, not at all. 一点都不。 There is no pre-named function for initialization in JavaScript.
在JavaScript中没有预先命名的初始化函数。 Typically, if you have need of a "class" of objects in JavaScript, you use a constructor function with the
new
keyword: 通常,如果您需要JavaScript中的“类”对象,则使用带有
new
关键字的构造函数:
function Person(first, last) {
this.first = first;
this.last = last;
}
// Usage:
var chuck = new Person("Charles", "Foreman");
You might then add functions that all instances created via new Person
can use, by adding them as properties to the prototype that gets assigned to objects created via new Person
(that prototype is taken from the Person.prototype
property): 然后,您可以添加通过
new Person
创建的所有实例可以使用的函数,方法是将它们作为属性添加到原型中,该原型将分配给通过new Person
创建的对象(该原型取自Person.prototype
属性):
Person.prototype.getFullName = function() {
return this.first + " " + this.last;
};
Because this is a bit long-winded and handling inheritance hierarchies is more effort than it is in other languages (something that's being fixed in ES6), there are a lot of libraries out there that provide helper functions to hook things up for you. 因为这有点啰嗦,处理继承层次结构比在其他语言中更加努力(在ES6中已经修复了),有很多库提供帮助函数来为您提供连接。 (Mine is called
Lineage
, there's also Resig's Really Simple Inheritance , PrototypeJS's Class
, etc.) Some of these helper scripts may give certain function names special meaning. (我的名字叫
Lineage
,还有Resig的Really Simple Inheritance ,PrototypeJS的Class
等等。)其中一些帮助程序脚本可能会给某些函数名称带来特殊含义。 For instance, in both Lineage
and PrototypeJS's Class
, a function called initialize
is special. 例如,在
Lineage
和PrototypeJS的Class
,一个名为initialize
是特殊的。 (I've seen some where ctor
was special.) (我已经看到了一些地方
ctor
是特殊的。)
But not within JavaScript itself. 但不是在JavaScript本身。
Your Zoo
might look like this: 您的
Zoo
可能如下所示:
function Zoo(animal_list) {
this.animals = animal_list.slice(0); // Copy the array
}
If you want to add functions available on all Zoo
instances, typically you'd add them to the prototype assigned by new Zoo
: 如果要添加所有
Zoo
实例上可用的函数,通常会将它们添加到由new Zoo
分配的原型中:
Zoo.prototype.showAll = function() {
this.animals.forEach(function(animal) {
console.log(animal);
});
};
// Usage:
var z = new Zoo(['Tortoise', 'Emu', 'Lion']);
z.showAll();
If you want to make your Zoo class a constructor it should be a function instead of an object literal: 如果你想让你的Zoo类成为构造函数,它应该是一个函数而不是一个对象文字:
var Zoo = function(animal_list) {
this.animals = [];
for (i = 0; i < animal_list.length; i++) {
this.animals.push(animal_list[i]);
}
}
var myZoo = new Zoo(["lions", "tigers", "bears"]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.