Initial exposure to object-oriented Javascript

Source: Internet
Author: User

I. object scope

Function classa () {var v = "I Am a variable v"; this. V = "I'm property v"; var me = This; classa. prototype. F = function () {alert ("I Am a function for external access");} function _ F () {alert ("I am just an internal function, external access to me is not allowed. ");} Classa. prototype. f2 = function () {alert (V); // OK alert (this. v); // OK _ F (); // OK f (); // This. F (); // OK} function _ F2 () {f (); // This. F (); // error me. F (); // OK} var obja = new classa (); alert (obja. v); // display "I Am a property v" obja. f2 (); obja. _ F2 (); // The system reports an error, indicating that the object is missing

Ii. Rewrite

Function classa () {var v = "I Am a variable v"; this. V = "I'm property v"; if (typeof classa. _ initialized = "undefined") {classa. prototype. F = function () {alert ("I Am a function for external access");} classa. _ initialized = true;} function _ F () {alert ("I am just an internal function and cannot access me externally. ") ;}} Var obja = new classa (); obja. V = "I am the property V, and now the bird is assigned a new value"; classa. prototype. F = function () {alert ("I Am a function that is accessible to external users and is now rewritten");} alert (obja. v); // display "I Am a property V, now being assigned a value again" obja. F (); // display "I Am a function that is accessible to external users and is now rewritten"

Function classa () {This. V = "I'm property v"; if (typeof classa. _ initialized = "undefined") {classa. prototype. F = function () {alert ("I Am a static function for external access F");} classa. _ initialized = true;} This. f2 = function () {alert ("I Am a function for external access F2") ;}} var obja1 = new classa (); var obja2 = new classa (); classa. prototype. F = function () {alert ("I Am a static function f that is accessible to external users. It is modified. ");} Obja1.f2 = function () {alert (" I Am a function F2 for external access, bird modified by obja1 ");} obja1.f (); // I am a static function f that is accessible to external users. Obja2.f (); // I am a static function f that can be accessed externally. Obja1.f2 (); // I am the function F2 that can be accessed externally, and obja1 modifies bird obja2.f2 (); // I am the function F2 that can be accessed externally.

Iii. Inheritance

Function classa () {This. va = "I am the attribute VA of classa"; if (typeof classa. _ initialized = "undefined") {classa. prototype. F = function () {alert ("I Am a static function f that classa can provide external access");} classa. _ initialized = true;} This. f2 = function () {alert ("I Am a function for external access F2") ;}} function classb () {This. VB = "I am the classb attribute VB";} classb. prototype = new classa (); var obja = new classa (); var objb = new classb (); alert (objb. va); // I am classa Attributes of vaalert (objb. VB); // The attribute vbobjb of classb. F (); // I am a static function fobjb that classa can provide external access. f2 (); // I am a function that can be accessed externally. f2classb. prototype. F = function () {alert ("I Am a static function f that classa can provide external access, and it is modified by classb. ");} Obja. F (); // I am a static function fobjb that classa can provide external access. F (); // I am a static function f provided by classa for external access, and it is modified by classb.

References:

Http://www.w3school.com.cn/js/pro_js_object_defining.asp

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.