JavaScript Object oriented this keyword usage analysis _javascript tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.