At least every try JavaScriptOO programmers spend a lot of energy to use on the object-oriented simulation mechanism and non-business itself.
this in Java, c + + Php developers even it is hard to imagine.
worse simulation OO appeal to the JavaScript senior programmers have evil.
because for the detachment on business, have created a new programming language, generally can make IQ brandish asperses.
as in previous years. Everybody wants to own website common js as a framework. Until YUI, JQuery, and so on the strength of the launch down slightly.
although each framework have to JavaScriptOO simulation, however, but not yet to have someone who can be a bucket of the paste.
maybe don't need overlord of the lake, or we just wait until JS2.0 +.
if can new is object oriented, that obviously JavaScript in this respect is very good.

code is as follows:

function Person (name) {
this. Name=name;
var lenel=new person (" lenel ");
alert (lenel. Constructor===Person);
alert (lenel instanceof Person===true).
alert (lenel instanceof Object===true).

so far, everything is harmonious.
the Object's constructor as the literal meaning to construct it Person.
Object is constructed its Person of an instance (instance).
all Object is the Object instance, much like Java.
JavaScript provide prototype (prototype) way to implement the method development and inheritance

code is as follows:

Person. The prototype. The getName=function () {
return this. The name

this defines all objects with the getName method.
of course can also be written in the object construction when

code is as follows:

function Person (name) {
this. Name=name;
this. GetName=function () {
return this. The name;

but the practice is not only to bring extra performance loss that flaws, also is not only the privilege of brought can access private variables.
it and use the prototype method there will be other differences, but this is not the focus of this article.
then, we think of inheritance, common writing is like this.

code is as follows:

function Stuff (name, id) {
this. Name=name;
this. Id=id;

} Stuff. The prototype=new Person ();
var piupiu=new Stuff (" piupiu ", "007");
alert (piupiu getName ());

is very good, inherited the getName method;
investigation under instanceof

code is as follows:

alert (piupiu instanceof Stuff===true).
alert (piupiu instanceof Person===true).

very well, the Stuff and Person are related. Piupiu is its two instances, very Java.
then inspects the constructor

code is as follows:

alert (piupiu. Constructor===Stuff);//false
test (piupiu constructor===Person);//true

problem for the new Stuff is what constructor is Person,
to truth, also argue irrationally, here we have to remember that conclusion
: constructor attribute of the object is not pointing to the constructor, but point to its constructor's prototype attribute constructor attribute
the poor writing ability, oneself read all don't think it's clear
object piupiu constructor attribute points to is the constructor of the constructor Stuff of prototype property attribute
because Stuff. The prototype=new Person ();
so Stuff. The prototype. The constructor===Person.
so piupiu consturctor===Person.
the above conclusion is not only the object inheritance when it appears, in defining objects

code is as follows:

function Student (name) {
this. Name=name;

} Student. The prototype={
getName: function () {
return this. The name;
elegantly-named setName: function (name) {
this. Name=name;


if the method is more, often like this, it looks some rules.
in fact the same writing at the expense of the constructor

code is as follows:

var moen=new Student (" moen ");
alert (moen. Constructor===Student);//false
alert (moen. Constructor===Object);//true

because in {} is equivalent to the new Object (), so according to the above conclusion prototype={}, constructor changed.
defend constructor! We use a loop to inherit the parent class method copyto subclass

code is as follows:

function Stuff1 (name, id) {
this. Name=name;
this. Id=id;

} for {(var fn in Person. The prototype)
Stuff1. The prototype (fn)=Person. The prototype (fn);
var piupiu1=new Stuff1 (" piupiu1 ", "008");
alert (piupiu1 getName ()==="piupiu1");
alert (piupiu1 constructor===Stuff1);

It works! When we excitedly to the parent class method is inherited, we lost a parent-child relationship.

code is as follows:

alert (piupiu1 instanceof Stuff1===true).//true
alert (piupiu1 instanceof Person===true).//false

apparently, we don't have said Stuff1 be inherited to the Person, only a for loop can explain what?
this is like a pair of contradictory.. Selected will be lost.
so shocked, further using the object, even you are not careful, can make instantceof and constructor to the literal meaning.
so shocked, at the time of using a framework, if you provide the function of inheritance, don't know if you don't try you give up in order to inherit which.
so shocked, when simulating OO will have to provide alternative to both the literal meaning of the realization of the properties or methods, and must let the user know!
simulation OO is absolutely not the problem, in the subclass method calls the superclass method of the same name, the common features of OO language,
JavaScript implementation is also very difficult, basic all have their own a set of masters, but some intention to do compared to use more natural, such as Base library.
of course I can abandon the instanceof and the use of the constructor, as eaches, don't know how so still can continue to work, won't die.
but if you are not alone, then inform your Partner also abandon the greats.
like today this thing, the veteran is clear, but if not everyone on the team have the unification understanding, that is hidden.
the phenomenon of a fun <<Senior JavaScript programming >>Emphasizes the instanceof, but <<Proficient in JavaScript> >Emphasizes the constructor. But their is another one has brought, or never mentions. Resentment.. Very deep...

you might be interested in the article:

  • realization principle, understand the instanceof Javascript_07_
  • javascript instanceof, the difference between the typeof
  • javascript instanceof internal mechanism analysis

This concludes the body part