I have something like that:
// original function
Foo = function(params) {
do foo...
}
Foo.prototype.alert = function() {
alert('foo');
}
Now i want to interfere:
Bar = Foo;
Foo = function(params) {
do bar...
return Foo(params);
}
Or the JQuery way:
(function() {
var proxied = Foo;
Foo = function() {
do bar...
return proxied.apply(this, arguments);
};
})();
The problem now is that Foo is missing all its prototype methods. Any idea how I could make this work?
jQuery.extend(Foo.prototype, proxied.prototype);
Or even:
Foo.prototype = proxied.prototype;
Related
my problem seems to be that my object function is not visible if i call it from within an object of functions. Example Code:
function foo()
{
this.bar = function()
{
alert("hit me!");
}
this.sna = {
fu: function ()
{
this.bar();
}
};
}
this seems to refer to sna instead of foo. How do i adress foo? this.parent does not work.
Use a variable to refer to this(Foo). See this - JavaScript | MDN
function Foo() {
this.bar = function() {
console.log("hit me!");
};
var that = this;
this.sna = {
fu: function() {
that.bar();
}
};
}
var foo = new Foo();
foo.bar();
foo.sna.fu();
One option is to add a reference to this:
function foo() {
var _t = this;
this.bar = function() { };
this.child = {
this.foo = function() {
_t.bar():
};
};
}
How can I access a method from a parent class that was overridden in the child class?
In My example below I want to call the bar.my_name() method inside the overriding
method in foo.my_name()
function bar() {
this.my_name = function() {
alert("I Am Bar");
}
}
function foo() {
this.my_name = function() {
alert("I Am Foo");
//access parent.my_name()
}
}
foo.prototype = Object.create(bar.prototype);
foo.prototype.constructor = foo;
var test = new foo();
test.my_name();
You could do this:
(new bar()).my_name.call(this);
I think you're a little confused about how prototypes work though, as they're not really helping you here.
This might be slightly better:
var bar = {
my_name: function () {
console.log('bar name');
}
};
var foo = Object.create(bar);
foo.my_name = function () {
console.log('foo name');
bar.my_name.call(this);
};
Or if you want to use constructors, something like this:
function Bar () {}
Bar.prototype.my_name = function () {
console.log('bar name');
};
var foo = Object.create(Bar.prototype);
foo.my_name = function () {
console.log('foo name');
bar.my_name.call(this);
};
But I'm not really sure what you're trying to do or why, so with more context it will be easier to give you better advice.
One of possible solutions is to move the method to the base class prototype.
function bar() {
}
bar.prototype.my_name = function() {
alert("I am bar");
}
function foo() {
}
foo.prototype = Object.create(bar.prototype);
foo.prototype.my_name = function() {
alert("I Am Foo");
bar.prototype.my_name.call(this);
}
foo.prototype.constructor = foo;
var test = new foo();
test.my_name();
var Foo = (function () {
var foo = function() { };
var privateMethod = function(){ };
foo.prototype = {
init: function() {
console.log(this.privateMethod); //undefined
}
};
return foo;
})();
I know that I can access privateMethod directly without using the this pointer. But since I come from the c# world, I would like to use it for readability purposes.
Is there any way to reference my "private methods" using a pointer?
You can't. You can only use this to refer to "public" methods. If you really want to use a something.method notation, you could use:
var Foo = (function () {
var foo = function() { };
var private = {
privateMethod : function(){ };
}
foo.prototype = {
init: function() {
console.log(private.privateMethod);
}
};
return foo;
})();
privateMethod is not specific to each instance of foo. Just reference it without the this. qualifier—although you probably want to log the results of a function call, not the function itself:
console.log(privateMethod());
This doesn't work, and I have no idea how to fix it
function bar() {...}
function foo() {
this = new bar();
this.newfunction = function() {...};
this.newvalue = "foobar";
}
var foobar = new foo();
Thanks in advance,
Do not use this to represent another object.
function bar() {...}
function foo() {
var bar = new bar();
bar.newfunction = function() {...};
bar.newvalue = "foobar";
}
var foobar = new foo();
Are you trying to inherit from bar? Then you can borrow its constructor and all its own properties using call (or apply):
function bar() {...}
function foo() {
bar.call(this);
this.newfunction = function() {...};
this.newvalue = "foobar";
}
var foobar = new foo();
How can I declare non-global static methods in js?
foo.bar = function() {
function testing{
console.log("statckoverlow rocks!");
}
function testing2{
console.log("testing2 funtction");
}
}
How can I call the testing functions? I am a newbie in JS.
Thanks for help.
You probably want an object.
foo.bar = {
testing: function() {
console.log("statckoverlow rocks!");
},
testing2: function() {
console.log("testing2 funtction");
}
};
Then, call foo.bar.testing(), for example.
You could do like this:
foo.bar = (function() {
var testing = function () {
console.log("statckoverlow rocks!");
};
var testing2 = function () {
console.log("testing2 funtction");
};
return {
testing: testing,
testing2: testing2
};
}());
// call them
foo.bar.testing();
foo.bar.testing2();
Did you mean:
var foo = {
bar: {
testing: function()
{
console.log("statckoverlow rocks!");
},
testing2: function()
{
console.log("testing2 funtction");
}
}
};
foo.bar.testing();
foo.bar.testing2();
// Constructor
function Foo() {
var myvar = 'hey'; // private
this.property = myvar;
this.method = function() { ... };
}
Foo.prototype = {
staticMethod: function() {
console.log( this.property );
}
}
var foo = new Foo();
foo.staticMethod(); //=> hey