This article analyzes the usage of this keywords for object-oriented JavaScript. Share it for your reference. The specific analysis is as follows:
When you need to initialize a certain attribute, you can use this keyword in the prototype object. As shown in the following example:
Copy the code as follows:<script language="javascript" type="text/javascript">
function Person(){
this.name = "My beauty in Wang";
this.age = 25;
};
var p1 = new Person();
var p2 = new Person();
document.writeln(p1.name+"<br />");
document.writeln(p2.name);
</script>
Note: The keyword this can only be used inside the prototype object. When used externally, this is equivalent to window. As shown in the following example
Copy the code as follows:<script language="javascript" type="text/javascript">
var name="Beijing";
window.alert(this.name);//Output "Beijing"
</script>
I hope this article will be helpful to everyone's JavaScript programming.