What is the meaning and usage of the init() function in JavaScript?
-
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– Lime2011-10-25 03:08:43 +00:00Commented Oct 25, 2011 at 3:08
-
3@LiamWilliam - initializeUpTheCreek– UpTheCreek2014-09-21 15:18:58 +00:00Commented Sep 21, 2014 at 15:18
5 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();
2 Comments
var bEvent = function () {...}(); and afterward, $(function () { // Launch functions bEvent.init() })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.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.
Comments
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.
1 Comment
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.
Comments
JavaScript always has to create the prototype from an object, so we create an object with a init() function in order to initialize the referred object with some pre-defined information.
Object.create() does not have a constructor. The most common pattern is an init function or constructor that must be created manually:
const userTest = {
init: function (name, id, email){
this.name = name;
this.id = id;
this.email = email;
},
result: function(){
return `${this.name}, ${this.id}, ${this.email}`;
}
}
const userTestDavid = Object.create(userTest);
userTest.init('David, 33333432109, [email protected]');
console.log(userTest.result());