What is the meaning and usage of the init()
function in JavaScript?

- 4,503
- 4
- 21
- 42

- 933
- 1
- 7
- 6
-
1init is just shorthand for initiate. Typically it is used to create a "new Object()". Like the init() function in jQuery returns a new jQuery object. – Lime Oct 25 '11 at 03:08
-
3@LiamWilliam - initialize – UpTheCreek Sep 21 '14 at 15:18
4 Answers
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.
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.
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.
function init() {
// initialisation stuff here
}
// elsewhere in code
init();

- 147,572
- 30
- 200
- 241
-
Is this still the case? I have some code now that does something like this: `var bEvent = function () {...}();` and afterward, `$(function () { // Launch functions bEvent.init() })` – Jeff Apr 15 '15 at 12:12
-
1@Jeff - Is what still the case? In your example, `bEvent` must refer to an object that has an `init() ` method, which you should see is created somewhere within the function body that you don't show. – nnnnnn Apr 16 '15 at 11:04
In JavaScript when you create any object through a constructor call like below
step 1 : create a function say Person..
function Person(name){
this.name=name;
}
person.prototype.print=function(){
console.log(this.name);
}
step 2 : create an instance for this function..
var obj=new Person('venkat')
//above line will instantiate this function(Person) and return a brand new object called 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 init will help in instantiating the object properties. basically init is like a constructor call on your class.

- 1,872
- 19
- 31

- 551
- 6
- 17
NB. Constructor function names should start with a capital letter to distinguish them from ordinary functions, e.g. MyClass
instead of myClass
.
Either you can call init
from your constructor function:
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.

- 150
- 7
-
1And please check http://stackoverflow.com/questions/3526916/javascript-class-call-method-when-object-initialized – Kiran Raj R May 10 '17 at 06:48
This is more like unreachable code
eg. if variable x
or function x()
is declared below the line where its
called this error generates:
setPlayerOne();
let imageGenerator = (value) =>{
allImages = {
1: 'images/dice1.png',
2: 'images/dice2.png',
3: 'images/dice3.png',
4: 'images/dice4.png',
5: 'images/dice5.png',
6: 'images/dice6.png',
}
return allImages[Number(value)];
}
findRandom = () =>{
let randomNumber = Math.floor(Math.random() * 6) +1
return randomNumber;
}
let setPlayerOne = () =>{
let img1 = document.querySelector('.img1').attributes.src;
img1.value = imageGenerator(findRandom())
}
let setPlayerTwo = () =>{
let img2 = document.querySelector('.img2').attributes.src;
img2.value = imageGenerator(findRandom())
}
setPlayerTwo();
setPlayerOne()
method will generate this but setPlayerTwo()
will not generate;
this because setPlayerOne()
was called before initialized by JS.