码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2017-10-24 18:19:12      阅读:259      评论:0      收藏:0      [点我收藏+]

标签:nim   color   ons   evel   https   lan   his   java   javascrip   

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()--应用

标签:nim   color   ons   evel   https   lan   his   java   javascrip   

原文地址:http://www.cnblogs.com/fangyinghua/p/7724774.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!