What is the use of the init() usage in JavaScript?

JavascriptInit

Javascript Problem Overview


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

Javascript Solutions


Solution 1 - Javascript

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();

Solution 2 - Javascript

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.

Solution 3 - Javascript

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.

Solution 4 - Javascript

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid S.View Question on Stackoverflow
Solution 1 - JavascriptnnnnnnView Answer on Stackoverflow
Solution 2 - JavascriptVenkatView Answer on Stackoverflow
Solution 3 - JavascriptKiran Raj RView Answer on Stackoverflow
Solution 4 - JavascriptProauView Answer on Stackoverflow