<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>test</title>
</head>
<body>
<script type="text/javascript" charset="utf-8">
(function(){
// this
var test=function(){
//this
return function(){
//this
};
}
(function(){
//this
var a={
p1:function(){
//this
}
};
})();
})();
</script>
</body>
</html>
<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd> <html> <head> <meta http-equiv=Content-type content=text/html; charset=utf-8
Share
David Dorward already mentioned about JavaScript: The Good Parts by Douglas Crockford.
From Section 4.3 of that excellent book:
Crockford continues to explains the binding of ‘this’ in each of these patterns, as follows:
The Method Invocation Pattern: When a function is stored as a property of an object, we call it a method. When a method is invoked, this is bound to that object.
The Function Invocation Pattern: When a function is invoked with this pattern, this is bound to the global object. This was a mistake in the design of the language.
The Constructor Invocation Pattern: If a function is invoked with the new prefix, then a new object will be created with a hidden link to the value of the function’s prototype member, and this will be bound to that new object.
The Apply Invocation Pattern: The apply method lets us construct an array of arguments to use to invoke a function. It also lets us choose the value of this. The apply method takes two parameters. The first is the value that should be bound to this. The second is an array of parameters.