An example of this article is an analysis of JavaScript object-oriented this keyword usage. Share to everyone for your reference. The specific analysis is as follows:
When you need to initialize a property, you can use this keyword within the prototype object. As in the following example:
Copy Code code as follows:
<script language= "javascript" type= "Text/javascript" >
function person () {
THIS.name = "Wang Meixin";
This.age = 25;
};
var p1 = new Person ();
var p2 = new Person ();
Document.writeln (p1.name+ "<br/>");
Document.writeln (P2.name);
</script>
Note: This keyword can only be used within a prototype object. Used externally, this is equivalent to window. As the following example
Copy Code code as follows:
<script language= "javascript" type= "Text/javascript" >
var name= "Beijing";
Window.alert (this.name);/output "Beijing"
</script>
I hope this article will help you with your JavaScript programming.