parent and children class each with own initialize

已查看 26 次
跳至第一个未读帖子

Patrik Gmitter

未读,
2012年11月2日 15:28:272012/11/2
收件人 jsclas...@googlegroups.com
How can i create parent class with initialize so that children class could have his own initialize method too

pseudo code

class A {
    initialize: consoloe.log('init in A');
}

class A extend B {
    initialize: consoloe.log('init in B');
}

new B;

CONSOLE OUTPUT

init in A
init in B

THANKS

James Coglan

未读,
2012年11月2日 17:02:142012/11/2
收件人 jsclas...@googlegroups.com
On 2 November 2012 19:28, Patrik Gmitter <pa...@patie.info> wrote:
How can i create parent class with initialize so that children class could have his own initialize method too

var Parent = new JS.Class({
  initialize: function() {
    console.log('A');
  }
});

var Child = new JS.Class(Parent, {
  initialize: function() {
    this.callSuper();
    console.log('B');
  }
});

   
已删除帖子

Patrik Gmitter

未读,
2012年11月2日 19:12:432012/11/2
收件人 jsclas...@googlegroups.com
right i missing about callSuper();
but cannot be writed this "switched" way - i wanna all in parent and i dont have to worry about something in childrens

thank you very much :)

James Coglan

未读,
2012年11月2日 19:18:132012/11/2
收件人 jsclas...@googlegroups.com
On 2 November 2012 23:12, Patrik Gmitter <pa...@patie.info> wrote:
right i missing about callSuper();
but cannot be writed this "switched" way - i wanna all in parent and i dont have to worry about something in childrens

I'm not sure what you mean. What code do you want to be able to right, and what should it do? 

Patrik Gmitter

未读,
2012年11月2日 19:29:372012/11/2
收件人 jsclas...@googlegroups.com
basicaly i need call function on parent init and need using init in children.

but all code like this.callSuper - if its possible move to parent not in children - for transparency and sure dont forget something.

James Coglan

未读,
2012年11月2日 19:51:252012/11/2
收件人 jsclas...@googlegroups.com
On 2 November 2012 23:29, Patrik Gmitter <pa...@patie.info> wrote:
basicaly i need call function on parent init and need using init in children.

but all code like this.callSuper - if its possible move to parent not in children - for transparency and sure dont forget something.

You can't do this -- if you override initialize() in the subclass you must explicitly call callSuper() to invoke initialize() in the parent class, if that's what you want. If you don't override initialize() in the subclass, it inherits the method from the parent.

Patrik Gmitter

未读,
2012年11月2日 20:07:442012/11/2
收件人 jsclas...@googlegroups.com
i dont know how but in backbone views something like this working.. within parent (view) extending some functions are called ...

https://github.com/documentcloud/backbone/blob/master/backbone.js#L1195

but its small OT

i look at it closer at morning. thank you
回复全部
回复作者
转发
0 个新帖子