How to set and run javascript constructor
Is it possible to create a constructor and have it run the code inside it, similar to languages like C# and Java? The code below is kind of what I am getting at.
For example
function Test()
{
Test.开发者_StackOverflowconstructor = function ()
{
/* Run code inside here when created */
}
}
var test = new Test();
Function Test can be the constructor itself if you call it with the new operator.
function Test() {
/* Run code inside here when created */
}
...
var test = new Test();
You can assign methods via:
Test.prototype.aMethod = function () {
/* Run code inside here when invoked */
}
test.aMethod();
Yeah and it's even simpler
function Test()
{
/* Run code inside here when created */
}
var test = new Test();
精彩评论