[javascript] What is the use of the JavaScript 'bind' method?

i did not read above code but i learn something in simple so want to share here about bind method after bind method we can use it as any normal method.

<pre> note: do not use arrow function it will show error undefined  </pre>

_x000D_
_x000D_
let solarSystem = {
    sun: 'red',
    moon : 'white',
    sunmoon : function(){
       let dayNight = this.sun + ' is the sun color and present in day and '+this.moon + ' is the moon color and prenet in night';
        return dayNight;
    }
}

let work = function(work,sleep){
    console.log(this.sunmoon()); // accessing the solatSystem it show error undefine sunmmon untill now because we can't access directly for that we use .bind()
    console.log('i work in '+ work +' and sleep in '+sleep);
}

let outPut = work.bind(solarSystem);
outPut('day','night')
_x000D_
_x000D_
_x000D_

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to function

$http.get(...).success is not a function Function to calculate R2 (R-squared) in R How to Call a Function inside a Render in React/Jsx How does Python return multiple values from a function? Default optional parameter in Swift function How to have multiple conditions for one if statement in python Uncaught TypeError: .indexOf is not a function Proper use of const for defining functions in JavaScript Run php function on button click includes() not working in all browsers

Examples related to bind

Docker Error bind: address already in use Update Angular model after setting input value with jQuery How to remove an appended element with Jquery and why bind or live is causing elements to repeat How to enable named/bind/DNS full logging? What does it mean to bind a multicast (UDP) socket? Detect user scroll down or scroll up in jQuery Cannot assign requested address using ServerSocket.socketBind jQuery bind/unbind 'scroll' event on $(window) What is the use of the JavaScript 'bind' method? Understanding ASP.NET Eval() and Bind()