function bind

Description

function bind() method creates a new function instance whose this value is bound to the value that was passed into bind().

Example


<!-- w w w. j  a v  a 2 s .c o m-->
<!DOCTYPE HTML>
<html>
<head>
  <script type="text/javascript">
    window.myName = "XML";
    var o = { myName: "CSS" };
    
    function printMyName(){
       console.log(this.myName);
    }
    var bindedPrintMyName = printMyName.bind(o);
    bindedPrintMyName();   //CSS
  </script>
</head>
<body>
</body>
</html>

Click to view the demo

bindedPrintMyName() is created from printMyName() by calling bind() and passing in the object o. this value in bindedPrintMyName() points to o. Calling bindedPrintMyName() in global context outputs "CSS" .





















Home »
  Javascript »
    Javascript Introduction »




Script Element
Syntax
Data Type
Operator
Statement
Array
Primitive Wrapper Types
Function
Object-Oriented
Date
DOM
JSON
Regular Expressions