Constructor and prototype in javascript

javascript

Solution

1st case : static method.

function Test() {}
Test.method = function () { alert(1); };
var t = new Test;
Test.method(); // alerts "1"
t.method(); // TypeError: Object #<Test> has no method 'method'

2nd case : instance method.

function Test() {}
Test.prototype.method = function () { alert(1); };
var t1 = new Test;
var t2 = new Test;
t1.method(); // alerts "1"
t2.method(); // alerts "1"
Test.method(); // TypeError: Object function Test() {} has no method 'method'

Problem

What is the difference between this two codes and which one should I use? ``` function Test() {} Test.method = function() {}; ``` With Prototype: ``` function Test() {} Test.prototype.method = function() {}; ```

Original source

Related problems