开发者

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜