Javascript Binding

Developer
Size
3,718 Kb
Views
22,264

How do I make an javascript binding?

What is a javascript binding? How do you make a javascript binding? This script and codes were developed by Dave on 17 September 2022, Saturday.

Javascript Binding Previews

Javascript Binding - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Javascript Binding</title>
</head>
<body> <script src="js/index.js"></script>
</body>
</html>

Javascript Binding - Script Codes JS Codes

'use strict';
var characters = ['Ned Stark', 'Cat Stark', 'Jamie Lanister', 'Theon Greyjoy'];
var starks = ['Ned Stark', 'Cat Stark', 'Filthy Stark'];
var isInArray = function isInArray(arr, item, x, y) { console.log('arr', arr); console.log('item', item); console.log('x', x); console.log('y', y); return arr.indexOf(item) > -1;
};
// Both render the same
// console.log(characters.filter(character => isInArray(starks, character)));
// in this case only 2 arguments are passed to `isInArray()` the arguments we specify
// console.log(characters.filter(isInArray));
// in this case filter calls `isInArray` with 3 arguments. `item`, `index` and the `array` (characters). It returns nothing of value because they are in the wrong order and it doesn't have access to `starks`.
// console.log(characters.filter(isInArray.bind(null, starks)));
// in this case the second argument `starks` is pushed in as the first argument to `isInArray` and is preceded by the other 3 in the eg above.
// so you get `starks`, `item`, `index` and the `array` (characters)
// Bind method creates a new function that will call the original method (isInArray) but with some of the arguments already fixed. (starks)
// The call to bind returns a function that will call isInArray() with `starks` as the first argument followed by any remaining arguments given to the bound function.
/*
`bind` for currying
*/
var greet = function greet(gender, age, name) { var salutation = gender === 'male' ? 'Mr.' : 'Ms.'; return age > 25 ? 'Hello, ' + salutation + ' ' + name + '.' : 'Hey ' + name + '!';
};
// We are passing null because we are not using the 'this' keyword in our greet function.
var greetAnAdultMale = greet.bind(null, 'male', 45);
var greetAYoungster = greet.bind(null, '', 16);
// console.log(greetAnAdultMale('John Hartlove')); // 'Hello, Mr. John Hartlove.'
// console.log(greetAYoungster('Alex')); // 'Hey, Alex.'
/*
`Bind` to set `this`
*/
var user = { data: [{ name: 'T. Woods', age: 37 }, { name: 'P. Mickelson', age: 43 }], showData: function showData() { console.log(this.data[0].name + ' ' + this.data[0].age); }, showData2: function showData2() { return function () { console.log(this.data[0].name + ' ' + this.data[0].age); }.bind(this); }
};
// Without bind this will fail, `Cannot read property 'data' of undefined`
var showDataVar = user.showData.bind(user);
showDataVar();
var showDataVar2 = user.showData2();
showDataVar2();
var cars = { data: [{ name: "Honda Accord", age: 14 }, { name: "Tesla Model S", age: 2 }]
};
// We can borrow the showData () method from the user object we defined in the last example.
// Here we bind the user.showData method to the cars object we just created.
cars.showData = user.showData.bind(cars);
cars.showData(); // Honda Accord 14
Javascript Binding - Script Codes
Javascript Binding - Script Codes
Home Page Home
Developer Dave
Username DaveOrDead
Uploaded September 17, 2022
Rating 3
Size 3,718 Kb
Views 22,264
Do you need developer help for Javascript Binding?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Dave (DaveOrDead) Script Codes
Create amazing video scripts with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!