js--call( )/apply()/bind()--应用

Posted 平静的学习

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js--call( )/apply()/bind()--应用相关的知识,希望对你有一定的参考价值。

https://developer.mozilla.org/en-US/docs/Web/javascript/Reference/Global_Objects/Function/call
function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = ‘food‘;
}

function Toy(name, price) {
  Product.call(this, name, price);
  this.category = ‘toy‘;
}

var cheese = new Food(‘feta‘, 5);
var fun = new Toy(‘robot‘, 40);
var animals = [
  { species: ‘Lion‘, name: ‘King‘ },
  { species: ‘Whale‘, name: ‘Fail‘ }
];

for (var i = 0; i < animals.length; i++) {
  (function(i) {
    this.print = function() {
      console.log(‘#‘ + i + ‘ ‘ + this.species
                  + ‘: ‘ + this.name);
    }
    this.print();
  }).call(animals[i], i);
}
function greet() {
  var reply = [this.person, ‘Is An Awesome‘, this.role].join(‘ ‘);
  console.log(reply);
}

var i = {
  person: ‘Douglas Crockford‘, role: ‘Javascript Developer‘
};

greet.call(i); // Douglas Crockford Is An Awesome Javascript Developer

 

 

 

以上是关于js--call( )/apply()/bind()--应用的主要内容,如果未能解决你的问题,请参考以下文章

js-apply call bind 浅析

手撸JS call,apply,bind

手撸JS call,apply,bind

js--call( )/apply()/bind()--应用

学些js call apply bind的新的收获,做个记录,希望大家多多指导

js call 以及apply