JavaScript 中的 init() 用法有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7884081/
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
What is the use of the init() usage in JavaScript?
提问by David S.
I just wanna know know the particular meaning and usage of the init() statement in JavaScript.
我只想知道 JavaScript 中 init() 语句的特定含义和用法。
回答by nnnnnn
JavaScript doesn't have a built-in init()
function, that is, it's not a part of the language. But it's not uncommon (in a lot of languages) for individual programmers to create their own init()
function for initialisation stuff.
JavaScript 没有内置init()
函数,也就是说,它不是语言的一部分。但是,个别程序员init()
为初始化内容创建自己的函数的情况并不少见(在许多语言中)。
A particular init()
function may be used to initialise the whole webpage, in which case it would probably be called from document.ready or onload processing, or it may be to initialise a particular type of object, or...well, you name it.
init()
可以使用特定函数来初始化整个网页,在这种情况下,它可能会从 document.ready 或 onload 处理中调用,或者它可能用于初始化特定类型的对象,或者……好吧,您可以命名它。
What any given init()
does specifically is really up to whatever the person who wrote it needed it to do. Some types of code don't need any initialisation.
任何给定的init()
具体做什么实际上取决于编写它的人需要它做什么。某些类型的代码不需要任何初始化。
function init() {
// initialisation stuff here
}
// elsewhere in code
init();
回答by Venkat
In JavaScript when you create any object through a constructor call like below
在 JavaScript 中,当您通过如下所示的构造函数调用创建任何对象时
step 1 : create a function say Person..
第 1 步:创建一个函数说 Person..
function Person(name){
this.name=name;
}
person.prototype.print=function(){
console.log(this.name);
}
step 2 : create an instance for this function..
第 2 步:为此函数创建一个实例..
var obj=new Person('venkat')
//above line will instantiate this function(Person) and return a brand new object called Person {name:'venkat'}
//上面这行将实例化这个函数(Person)并返回一个名为Person {name:'venkat'}的全新对象
if you don't want to instantiate this function and call at same time.we can also do like below..
如果你不想实例化这个函数并同时调用。我们也可以像下面这样..
var Person = {
init: function(name){
this.name=name;
},
print: function(){
console.log(this.name);
}
};
var obj=Object.create(Person);
obj.init('venkat');
obj.print();
in the above method initwill help in instantiating the object properties. basically init is like a constructor call on your class.
在上面的方法中init将有助于实例化对象属性。基本上 init 就像对您的类的构造函数调用。
回答by Kiran Raj R
NB. Constructor function names should start with a capital letter to distinguish them from ordinary functions, e.g. MyClass
instead of myClass
.
注意。构造函数名称应以大写字母开头,以区别于普通函数,例如MyClass
代替myClass
。
Either you can call init
from your constructor function:
您可以init
从构造函数调用:
var myObj = new MyClass(2, true);
function MyClass(v1, v2)
{
// ...
// pub methods
this.init = function() {
// do some stuff
};
// ...
this.init(); // <------------ added this
}
var myObj = new MyClass(2, true);
function MyClass(v1, v2)
{
// ...
// pub methods
this.init = function() {
// do some stuff
};
// ...
this.init(); // <------------ added this
}
Or more simply you could just copy the body of the init
function to the end of the constructor function. No need to actually have an init
function at all if it's only called once.
或者更简单地说,您可以将init
函数体复制到构造函数的末尾。init
如果只调用一次,则根本不需要实际拥有一个函数。