我想了解.bind并制作以下代码:
一个简单的对象:
<?php
$datestring = (isset($_POST['datetimepicker1'])) ? $_POST['datetimepicker1'] : "not";
$datestring = str_replace('/', '-', $datestring);
$date = date("y/d/m", strtotime($datestring));
echo $datestring;
echo var_dump($date);
一个功能:
person = {
name:"Joe",
surname:"Something",
tool:"gun",
action: function(){
console.log("shoot my wife");
}
}
并将person对象绑定到警察功能
function police(){
console.log("You are under arrest, " + this.name + " " + this.surname);
}
最后我控制台登录:
var newPolice = police.bind(person);
我确实得到了所需的字符串(&#34;你被捕,Joe Something&#34;)但我也得到了一个未定义的,我不知道它来自哪里。 (在代码中,它是生成未定义的console.log(newPolice())
答案 0 :(得分:4)
这与bind
无关。
当您致电newPolice()
时,会记录下结果:
console.log("You are under arrest, " + this.name + " " + this.surname);
当您致电console.log( newPolice() );
时,您现在有两个 console.log
语句,它们之间是日志:
newPolice
newPolice
没有return
语句,因此会返回undefined
。