收录日期:2020/05/27 17:28:26 时间:2014-05-21 15:57:03 标签:javascript,class,object

I'm trying to generate a class from an object in JavaScript. For example:

var Test = {
    constructor: function() { document.writeln('test 1'); },
    method: function() { document.writeln('test 2'); }
};

var TestImpl = function() { };
TestImpl.prototype.constructor = Test.constructor;
TestImpl.prototype.method = Test.method;

var x = new TestImpl();
x.method();

But this doesn't work: it'll only write 'test 2' (for whatever reason, constructor isn't being defined properly). Why?

I think you're doing it wrong.

Remember, JavaScript doesn't actually have classes at all. It has prototypes instead. So what you're really trying to do is create a prototype object that works like a collection of functions that you've built on another object. I can't imagine any useful purpose for this -- could you elaborate as to what you're trying to do?

Although I think you could make it work by using something like:

var TestImpl = function() {
    Test.constructor.apply(this);
};
TestImpl.prototype.method = Test.method;

Your TestImpl function is the constructor. Usually you would do something like this:

var Test1 = function () {
  document.writeln('in constructor');
};

Test1.prototype = {
  x: 3,
  method1: function() { document.writeln('x='+this.x); }
}

var y1 = new Test1();
y1.method1();
y1.x = 37;
y1.method1();

var y2 = new Test1();
y2.method1();
y2.x = 64;
y2.method1();

I think you have things a little backwards. Usually you will assign a prototype to a constructor, rather than assigning a constructor to a prototype.

The reason for assigning a method to the constructor's prototype, rather than to the "this" object inside the constructor, is that the former method creates only 1 shared function, whereas the latter method creates separate instances of a function. This is important (to keep memory allocation to a reasonable amount) if you create lots of objects each with lots of methods.

var Test = function () {
  document.writeln('test 1');
  this.method = function() { document.writeln('test 2'); }
};

var x = new Test();
x.method();

Javascript doesn't have a "Class" concept, It's all about prototype and the way you use them [ and you can simulate any kind of inheritance with this little neat feature. ] In javascript "Function" plays the role of [ Class, Method and Constructor ].

so inorder to create "Class" behaviour in Javascript all you need to do is to use the power of "Function".

var A = function(){
    alert('A.Constructor');
}

A.prototype = {
    method : function(){
        alert('A.Method');
    }
}

var b = new A(); // alert('A.Constructor');
b.method(); // alert('A.Method');

now the neat thing about JS is that you can easily create "Inheritance" behaviour by using the same method. All you need to do is to connect the second class "Prototype Chain" to the first one, How?

B = function(){
 this.prototype = new A(); // Connect "B"'s protoype to A's
}
B.prototype.newMethod = function() { alert('testing'); }

var b = new B();

b.method();    // Doesn't find it in B's prototype, 
               // goes up the chain to A's prototype

b.newMethod(); // Cool already in B's prototype

// Now when you change A, B's class would automatically change too
A.prototype.method = function(){ alert('bleh'); }
b.method();    // alert('bleh')

If you need any more references I suggest to take a look at Douglas Crockford's Site Happy JS ing.