Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
</body>
</html>
 
var Multipler = function(inc){
  this.inc = inc;
}
// Not gonna work
Multipler.prototype.multiple = function(numbers){
  return numbers.map(function(number){
    return this.inc * number;
  })
} 
// => [NaN, NaN, NaN, NaN]
console.log(new Multipler(2).multiple([1,2,3,4]))
// cache this or using bind
Multipler.prototype.multiple = function(numbers){
  var self = this;
  return numbers.map(function(number){
    return self.inc * number;
  })//or  .bind(this)
}
//=> [2, 4, 6, 8]
console.log(new Multipler(2).multiple([1,2,3,4]))
// BUT with arrow function, this will be lexical
Multipler.prototype.multiple = function(numbers){
  return numbers.map((number) => number*this.inc);
};
console.log(new Multipler(2).multiple([1,2,3,4]));// => [ 2, 4, 6, 8 ]
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
jcouyangpro
0viewers