How to define classes in JavaScript (four ways) _javascript skills

Source: Internet
Author: User

The examples in this article describe how classes are defined in JavaScript. Share to everyone for your reference, specific as follows:

The definition of a class consists of four different ways:

1. Factory mode

function Createcar (name,color,price) {
  var tempcar=new Object;
  Tempcar.name=name;
  Tempcar.color=color;
  Tempcar.price=price;
  Tempcar.getname=function () {
   document.write (this.name+ "-----" +this.color+ "<br>");
  return tempcar;
}
var car1=new createcar ("Factory Santana", "Red", "121313");
Car1.getname ();

The definition of a factory function that creates and returns a specific type of object looks good, but there's a small problem

To create a new function Showcolor every time we call, we can move it outside the function,

function GetName () {
  document.write (this.name+ "-----" +this.color+ "<br>");
}

Point directly to it in the factory function

Copy Code code as follows:
Tempcar.getname = GetName;

This avoids the problem of duplicating the function, but it does not look like an object's approach.

2. Constructor mode

function car (name,color,price) {
  this.name=name;
  This.color=color;
  This.price=price;
  This.getcolor=function () {
   document.write (this.name+ "-----" +this.color+ "<br>");
  }
var car2=new car ("Structural Santana", "Red", "121313");
Car2.getcolor (); 

You can see the difference from the first method, where there are no objects created inside the constructor, but instead use the This keyword.

When you call a constructor with new, you create an object and then use this to access it.

This usage is similar to other object-oriented languages, but it has the same problem with the previous one, that is, repeating the creation of functions.

3. Prototype mode

function Procar () {
}
procar.prototype.name= "prototype";
Procar.prototype.color= "Blue";
Procar.prototype.price= "10000";
Procar.prototype.getname=function () {
  document.write (this.name+ "-----" +this.color+ "<br>");
var car3=new procar ();
Car3.getname ();

The constructor car is first defined, but there is no code, and then the property is added through prototype. Advantages:

A. All instances hold pointers to Showcolor, which solves the problem of duplicate creation of functions

B. You can check object types with instanceof

Copy Code code as follows:
Alert (car3 instanceof Procar);//true

Disadvantage, add the following code:

ProCar.prototype.drivers = NewArray ("Mike", "Sue");
Car3.drivers.push ("Matt");
alert (car3.drivers);//outputs "Mike,sue,matt"
alert (car3.drivers);//outputs "Mike,sue,matt"

Drivers is a pointer to an array object, and all two instances of Procar point to the same array.

4. Dynamic Prototyping mode

function Autoprocar (name,color,price) {
  this.name=name;
  This.color=color;
  This.price=price;
  This.drives=new Array ("Mike", "Sue");
  if (typeof autoprocar.initialized== "undefined") {
   autoProCar.prototype.getName =function () {
   document.write (this.name+ "-----" +this.color+ "<br>");
   Autoprocar.initialized=true
  }
}
var car4=new autoprocar ("Dynamic Prototype", "Yellow", "1234565");
Car4.getname ();
Car4.drives.push ("Newone");
document.write (car4.drives);

This is my favorite, all class definitions are done in a function that looks very much like a class definition in other languages, does not create functions repeatedly, and can be used with instanceof

I hope this article will help you with 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.