TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

JavaScript Function bind() Method

JavaScript Function bind() Method with example on javascript function object, javascript tutorial, apply(), bind(), call(), toString(), toSource(), isgenerator(), javascript handler, javascript reflect, object, number etc.

<< Back to JAVASCRIPT

JavaScript Function bind() method

The JavaScript Function bind() method is used to create a new function. When a function is called, it has its own this keyword set to the provided value, with a given sequence of arguments.

Syntax

function.bind(thisArg [, arg1[, arg2[, ...]]]

Parameter

thisArg - The this value passed to the target function.

arg1,arg2,....,argn - It represents the arguments for the function.

Return Value

It returns the replica of the given function along provided this value and initial arguments.

JavaScript Function bind() method Example

Example 1

Let's see a simple example of bind() method.

<script>
var website = {
  name: "TheDeveloperBlog",
  getName: function() {
    return this.name;
  }
}
var unboundGetName = website.getName;
var boundGetName = unboundGetName.bind(website);
document.writeln(boundGetName());
</script>
Test it Now

Output:

TheDeveloperBlog

Example 2

Let's see an example of bind() method.

<script>
// Here, this refers to global "window" object
this.name = "Oracle";     
var website = {
  name: "TheDeveloperBlog",
  getName: function() { return this.name; }
};

document.writeln(website.getName()); // TheDeveloperBlog

//It invokes at global scope
var retrieveName = website.getName;
document.writeln(retrieveName());   //Oracle

var boundGetName = retrieveName.bind(website);
document.writeln(boundGetName()); // TheDeveloperBlog
</script>
Test it Now

Output:

TheDeveloperBlog Oracle TheDeveloperBlog





Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf