Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Add functions to another ones

Reply
Thread Tools

Add functions to another ones

 
 
Archos
Guest
Posts: n/a
 
      02-18-2012
Is there any way for that a function can be added/embedded to a
constructor?

function hello() {console.log("hello")}
function Sum(x, y) { this.a=a; this.b=b; }

In that example I would want that function hello() can be added to
Sum()
 
Reply With Quote
 
 
 
 
tema
Guest
Posts: n/a
 
      02-18-2012
On Feb 18, 3:42*pm, Archos <(E-Mail Removed)> wrote:
> Is there any way for that a function can be added/embedded to a
> constructor?
>
> function hello() {console.log("hello")}
> function Sum(x, y) { this.a=a; this.b=b; }
>
> In that example I would want that function hello() can be added to
> Sum()


function hello() {console.log("hello")}
function Sum(x, y) { this.a=x; this.b=y; }

Sum = (function(SumOld) {
hello();
return SumOld;
})(Sum);

var h = new Sum(2, 3);
console.log(h);

http://jsfiddle.net/WS4fz/
 
Reply With Quote
 
 
 
 
tema
Guest
Posts: n/a
 
      02-18-2012
On Feb 18, 3:42*pm, Archos <(E-Mail Removed)> wrote:
> Is there any way for that a function can be added/embedded to a
> constructor?
>
> function hello() {console.log("hello")}
> function Sum(x, y) { this.a=a; this.b=b; }
>
> In that example I would want that function hello() can be added to
> Sum()


function hello() {console.log("hello")}
function Sum(x, y) { this.a=x; this.b=y; }

Sum = (function(SumOld) {
function construct(constructor, args) {
function F() {
return SumOld.apply(this, args);
}
F.prototype = constructor.prototype;
return new F();
}

return function() {
hello();
return construct(SumOld, [2, 3]);
}
})(Sum);

var h = new Sum(2, 3);
console.log(h);

http://jsfiddle.net/agentcooper/qLMUz/
 
Reply With Quote
 
tema
Guest
Posts: n/a
 
      02-18-2012
On Feb 18, 3:42*pm, Archos <(E-Mail Removed)> wrote:
> Is there any way for that a function can be added/embedded to a
> constructor?
>
> function hello() {console.log("hello")}
> function Sum(x, y) { this.a=a; this.b=b; }
>
> In that example I would want that function hello() can be added to
> Sum()


Here you go http://jsfiddle.net/agentcooper/qLMUz/
 
Reply With Quote
 
Archos
Guest
Posts: n/a
 
      02-18-2012
On Feb 18, 2:00*pm, tema <(E-Mail Removed)> wrote:
> On Feb 18, 3:42*pm, Archos <(E-Mail Removed)> wrote:
>
> > Is there any way for that a function can be added/embedded to a
> > constructor?

>
> > function hello() {console.log("hello")}
> > function Sum(x, y) { this.a=a; this.b=b; }

>
> > In that example I would want that function hello() can be added to
> > Sum()

>
> Here you gohttp://jsfiddle.net/agentcooper/qLMUz/


Thanks. Although I thought that I could be done easily using 'apply'
or a similar function.
 
Reply With Quote
 
Asen Bozhilov
Guest
Posts: n/a
 
      02-18-2012
tema wrote:

> > function hello() {console.log("hello")}
> > function Sum(x, y) { this.a=a; this.b=b; }

>
> > In that example I would want that function hello() can be added to
> > Sum()

>
> Here you gohttp://jsfiddle.net/agentcooper/qLMUz/


In first place it would be nice if the next time you post your code to
be directly here. It is definitely easier to quote different parts of
your code if it is in messages. Of course you could post here and post
link in jsfiddle. Also if jsfiddle removes your code the archive of
the group will store a broken link. In the last place not everyone is
on the web in newsgroup like that.

Such a design is better to avoid. I would use wrapper or subclassing
`Sum' instead of overwriting the constructor. Your approach after
overwriting the `Sum' with new function If I would like to extend the
`Sum.prototype', I would have a big problem. The `Sum' is not longer
constructor, it is factory for overwritten `Sum'.

Sum.prototype.missedLogic = function () {
console.log('Missed logic');
};

var h = new Sum(2, 3);
h.missedLogic(); //TypeError

If you and the author want real overwritten constructor, you could
use:

function hello() {console.log("hello")}
function Sum(x, y) { this.a=x; this.b=y; }


Sum = (function () {
var _Sum = Sum;
function SumWrapper() {
_Sum.apply(this, arguments);
hello();
}
SumWrapper.prototype = new _Sum;
return SumWrapper;
})();


 
Reply With Quote
 
John G Harris
Guest
Posts: n/a
 
      02-18-2012
On Sat, 18 Feb 2012 at 04:42:22, in comp.lang.javascript, Archos wrote:
>Is there any way for that a function can be added/embedded to a
>constructor?
>
>function hello() {console.log("hello")}
>function Sum(x, y) { this.a=a; this.b=b; }
>
>In that example I would want that function hello() can be added to
>Sum()


Have you realised that you've been asking some very peculiar questions.
Have you spent some time working out what you want to do, without
worrying about how to do it ?

If you have, have you understood the way prototypes work and how they
can help you ?

John
--
John Harris
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hard drives--Are big ones more delicate than smaller ones? PowerPost2000 Computer Support 2 03-01-2005 10:30 PM
is there an OSPF comman to list all ones areas? William J King Cisco 1 01-04-2005 08:22 PM
Ones Counter Paulo Valentim VHDL 0 10-20-2004 11:18 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Free MCSE Study by Microsoft (Resource kits) for ones who dont know yet ADRENALINE MCSE 1 01-30-2004 02:18 PM



Advertisments