Clarify JavaScript object-oriented ideas

Source: Internet
Author: User
I used this article to clarify my own ideas, and hope to clarify my ideas for those who have the same understanding as me.

I used this article to clarify my own ideas, and hope to clarify my ideas for those who have the same understanding as me. The error code is as follows:

var o = {    ...}var obj = new o();

Needless to say, of course, an error is returned. Unfortunately, I used to think that var o = {} is a class called o. Then, I managed it myself. Conclusion:

Var o = {}; equivalent to var o = new Object (); but not equivalent to var o = function (){};

The first two forms o is an Object, an Object class Object. The third form is a function, and more importantly, o is a class.

var o = {};o.oField = "oField";

Equivalent

var o = {oField : "oField"} 

The question is: How can attributes be directly defined on objects?

Because o is an Object class Object, o. prototype is undefined, so o. prototype. oField =...

In addition, I wrote two test codes and pasted them together. It can be a note.

// Define the class var Engin = function () {}; // instance attribute Engin. prototype. objectField = "objectField"; // class attribute (static field) Engin. classField = "classField"; // instance method Engin. prototype. objectMethod = function () {document. write ("objectMethod is called
");} // Class method (static method) Engin. classMethod = function () {document. write (" classMethod is called
");} // Call the instance method new Engin (). objectMethod (); // call the class method Engin. classMethod (); document. write (new Engin (). objectField +"
"); Document. write (Engin. classField +"
"); // Only class attributes and class methods can be traversed. // how can I traverse instance attributes and instance methods? Document. write ("use for in to traverse Engin objects ==============================
"); For (var o in Engin) {document. write (o +"
");} Document. write ("============================================ ============================
");
// Define the Parent and define an attribute pField and a method pMethod var Parent = function () {this. pField = "pField"; this. pMethod = function () {document. write ("pMethod is called
") ;}}; // Defines the static attribute Parent of the Parent class. staticPField = "staticPField"; // defines the static Parent of the Parent class. staticPMethod = function () {document. write ("staticPMethod is called
");} // Defines the subclass Child, and defines an attribute cField and a method cMethod var Child = function () {this. cField = "cField"; // instance property this. cMethod = function () {// instance method document. write ("cMethod is called
") ;}}; // Defines the static property Child of the subclass. staticCField = "staticCField"; // defines the static Child method of the subclass. staticCMethod = function () {document. write ("staticCMethod is called
");} // Specify that Child inherits from Parent Child. prototype = new Parent (); // create a subclass object var childObj = new Child (); document. write (childObj. pField +"
"); // The Sub-class Object accesses the attributes of the parent class instance // document. write (childObj. staticPField +"
"); // The subclass object cannot access the static attribute childObj of the parent class. pMethod (); // subclass object calls the parent class instance method // childObj. staticPMethod (); // a subclass object cannot call the static document method of the parent class. write (childObj. cField +"
"); // The subclass object accesses its own instance attribute document. write (Child. staticCField +"
"); // You must use the class name to access your own static attribute childObj. cMethod (); // The subclass object calls its own instance method Child. staticCMethod (); // you must use a class name to call your own static method.

This article is available at http://www.nowamagic.net/librarys/veda/detail/337.

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.