JavaScript defines classes in a way that differs from other OO languages

Source: Internet
Author: User

JavaScript Object-oriented programming has some discrepancies with other OO languages, so there are some normative issues to be aware of when using JavaScript's object-oriented features. Here's a quick look at how JavaScript defines a class, and how you need to standardize your code as you define the class. Dingzhou Technology Industry Bureau

The specification for defining classes using JavaScript is as follows:

Specifies the class name and constructor, the first letter of the class name (constructor name):

function YourClass () {}

Use the this. Member variable to define (pseudo) private members within its constructor, preferably a contract (pseudo) private member consisting of a lowercase letter beginning with "_". This member is a different copy of each object, also called an object (instance) member.

function YourClass (_arg1,_arg2,...) {      this._arg1=arg1;      THIS._ARG2=ARG2;      //...}

Use the class name. prototype member variable to define a member variable outside its constructor, preferably by agreeing that such a member begins with an uppercase letter (or the best convention (pseudo) Private member consists of a lowercase letter beginning with "_"). )。 This member variable is a copy, also called a class member, that each object shares.

Yourclass.prototype.arg3= "Arg3 ...";//define Direct access do not enter validation member variable yourclass.prototype._arg4= "Arg4 ...";//need to use SETXXX () GetXXX () accessors do input validation

Use the class name. prototype. member function name =function (_arga,_argb,...) {} "way to member functions.

Yourclass.prototype.yourfucname=function (_arga,_argb,...) {       //do somethings}

Do not use the "this" function within the constructor. =function (_arga,....) {} "means to define member functions, which are service templates, need to be shared, and do not need to have an identical template for each object, which is wasteful and does not make much sense.

JavaScript defines classes in a way that differs from other OO languages

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.