JavaScript中的“绑定”和“调用”方法有什么区别

时间:2019-03-14 05:17:49

标签: javascript

var vehicleName = 'Toyota';


vehicle.printVehicleNameInner();

var vehicle = {
  vehicleName: 'Nissan',
  printVehicleNameInner : function () {
    console.log("1. "+this.vehicleName);
    return function (){
        console.log("2. "+this.vehicleName);
    }
  }
};

var execute = vehicle.printVehicleNameInner();
//execute();   // Output    1.Nissan   2.Toyota

//execute.call(vehicle);    //Output    1.Nissan    2.Nissan

//execute.bind(vehicle);      //Output    1.Nissan

var exe = vehicle.printVehicleNameInner().bind(vehicle);
exe();            //Output    1.Nissan    2.Nissan

0 个答案:

没有答案