每天CookBook之JavaScript-048
- bind的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>048</title>
</head>
<body>
</body>
<script type="text/javascript">
(function () {
window.onload = function(){
window.name = "window";
var newObject = {
name : "object",
sayGreeting: function(){
console.log("Now this is easy, " + this.name);
nestedGreeting = function(greeting){
console.log(greeting + " " + this.name);
}.bind(this);
nestedGreeting("hello");
}
};
newObject.sayGreeting("hello");
}
})();
</script>
</html>