In fact, the beginning of compiling JS did not how to use the object, generally used Func,func,func But with more, feel the code is not beautiful, but also here to package a function, where the package a function, or has always been a function call, not good-looking, and some of the re-use of all to rewrite the words are very troublesome (sorry, for me this novice, start or general use Func more efficient ...) )。 So decided to start to use object to program to 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 ways to declare: 1, var test = function () {};2, function test () {};3, var test = new Object (), 4, var test = {}; All I can think of now is these four kinds of things, trouble you big God still have what can declare object of continue to supplement, Brother here thanked!
To separate the method at this time:
For the 1, 2 methods, there should be some existing properties, as if you give Test.name added a value, but you find the output test.name when the name of the output func, but test can add other static properties, but there are no other reserved words, for the moment I have not continued to find;
For the 3, 4 method, it is possible to assign a value to the test.name, and it will output the value correctly.
Therefore, for Func intrinsic properties, if the Func method is used to declare an object, it is possible that some values are not allowed, so the individual is more likely to recommend the object declaration using the 3, 4 method. However, the benefit of declaring a class with Func is that it is possible to initialize some basic values more intuitively.
Then there is the inheritance of the class, and here's an example
varParent =function(name,age) { This. Name =name; This. Age =Age ; This. method =function() {alert ("This is my method!"); }}//class InheritancevarChild =function(name,age) {parent.apply ( This, arguments);//or Test.call (this,name); this.age = age; //Other method or variables}//one of the benefits of this inheritance is that you can instantiate the parent class directly within the subclass, and the parent class's variable can be used directly in the subclass as long as it is in the Func body. However, instantiation within subclasses is not surefire plan, so let's look at/*---------------------------------------------------------------------------*/varParent =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!"); },}varChild =function(name,age) { This. Name =name; This. Age =Age ;}//class InheritanceChild.prototype =Newparent ();varc =NewChild ("Li", 20); C.run ();//There is alert, If the Parent.apply method is used in subclasses like before, then the content of Parent.prototype is not useful for child, so I think this is the subclass definition parent class (parent.apply) and the Outer subclass prototype (child.prototype = The difference between new parent ())
The above is my rookie of some of the learning content, I hope there are any mistakes can get you to correct me! Thank you!
Some experiments that the JavaScript class inherits