Some experiments on JavaScript class inheritance

Source: Internet
Author: User
Tags add object inheritance variables advantage


In fact, the beginning of a set of JS not how to use the object, generally used Func,func,func But with a lot of it, feel the code is not beautiful, but also to package a function, where the package a function, or has been a function call, not good-looking, and some reuse to rewrite the words will be very troublesome (sorry, for me this novice, beginning or general use Func more efficient ... )。 So decided to start using object to program can be more convenient, the following is I read some blog article about the class of opinion, there is nothing wrong hope you can give a lot of advice:



For class programming, there are several declared methods: 1, var test = function () {};2, function test () {};3, var test = new Object (); 4, var test = {}; Now I can think of only these four kinds, trouble you big God still have what can declare object of continue to add, younger brother here thanked!



To separate the method at this point:



For 1, 2 methods, there should be some existing attributes, as if you add a value to Test.name, but you find output test.name is the output of the Func name, but test can add other static properties, but there are other reserved words, temporarily I have not continued to find;



For 3, 4 methods, it is possible to assign values to Test.name, and the value is output correctly.



Therefore, for Func intrinsic properties, if the Func method is used to declare the object may be some values are not allowed, therefore, the individual more recommended object declaration with 3, 4 methods. However, the advantage of declaring a class with Func is that it is possible to initialize some basic values more intuitively






Then there's the inheritance of the class, and here's an example


var parent = function (name, age) {
  this.name = name;
  this.age = age;
  this.method = function () {
    alert ("This is my method!");
  }
}
// Class inheritance

var child = function (name, age) {
  parent.apply (this, arguments); // OR test.call (this, name); this.age = age;
  // Other method or variables
}
// This kind of inheritance actually has the advantage that the parent class can be instantiated directly in the subclass, and the variables of the parent class can be used directly in the subclass as long as they are in the func body. However, instantiation within subclasses is not a perfect solution.

/ * ------------------------------------------------ --------------------------- * /

var parent = function (name, age) {
  this.name = name;
  this.age = age;
}
parent.prototype = {
  run: function () {
    alert (this.name + "is running!");
  },
  work: function () {
    alert (this.name + "is working!");
  },
}

var child = function (name, age) {
  this.name = name;
  this.age = age;
}
// Class inheritance
child.prototype = new parent ();

var c = new child ("li", 20);
c.run (); // Alert, if the parent.apply method is used in the subclass as before, the content of parent.prototype has no effect on the child, so I think this is the parent class defined in the subclass The difference between parent.apply) and the outer subclass prototype (child.prototype = new parent ()) 

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.