Apply 与 Call 的用法(简化版)

时间:2023-03-09 17:10:14
Apply 与 Call 的用法(简化版)

Javascript中Apply 与 Call 的用法,这里只作简单的入门说明

function human( name, age ){
  this.name = name;
  this.age = age;
}
function Boy( name, age, sex ){
  human.apply( this, arguments );   // arguments 获得所有参数,数组形式 [ name, age, sex ]
  // human.call( this, name, age, sex );  // 使用 call 获得 同样结果
  this.sex = sex;
}
var XiaoMing = new Boy('小明', 18, '男');
for( var item in XiaoMing ){   // 遍历对象
  document.write(XiaoMing[item] + ',');
}