var Cow = function(name) { this.name = name; } Cow.prototype.moo = function() { document.getElementById('output').innerHTML += this.name + ' moos' + '<br>'; } var cow1 = new Cow('alice'); var cow2 = new Cow('bob'); cow1.moo();
<div id="output" /> <script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
Unfortunately, the actual βbind allβ functionality only works with functions directly on the object. To enable the function defined in the prototype, you need to pass these function names as additional arguments to _.bindAll() .
Anyway, you need an explanation: basically it allows you to replace a function on an object with a function that has the same name and behavior, but is also bound to that object, so this === theObject without even calling it as a method ( theObject.method() ).
ThiefMaster Jul 05 2018-11-11T00: 00Z
source share