Lets say we have function test():
function test(){
a();
this.b = function(){
alert(1);
}
function a(){
this.b();
}
}
var t = new test();
This code will throw TypeError: this.b is not a function
The question is, how can we correctly access b() from within a()?
Change the order:
function test(){
var me = this;
this.b = function(){
alert(1);
}
a();
function a(){
me.b();
}
}
You can't call this.b() before you've assigned the variable. And you need to use a local variable to capture the value of this in the closure.
function test(){
var me = this;
this.b = function () {
console.log(1);
}
function a() {
console.log('calling me.b()');
me.b();
}
a();
}
var t = new test();
console.log('calling t.b()');
t.b();
Related
How do I call function "a" from "a.v" without a direct reference "a()"
function a(){
alert("1");
this.v=function(){alert("hi")};
}
One way I can think of doing this without repeating a:
function a(){
let indirect = arguments.callee;
alert("1");
indirect.v=function(){ indirect(); };
}
a();
a.v();
But this does not work under strict mode and you must call a before calling a.v.
You could also do:
function a(){
alert('1');
this.v = () => this();
}
a.v = a;
a.v();
This way you don't call a() and it also works under strict mode.
Set its v property outside the function:
function a() {
alert("1");
}
a.v = function() {
a()
alert("hi")
};
a.v()
I am wondering how to access a function in camera.
I have no clue to access function a() and b() from outside.
When I run this code, I have undefined error in console.
var camera = (function () {
function a(){
console.log('calling ..a');
}
function b(){
console.log('calling ..b');
}
})();
//calling function a() from outside
camera.a();
You can return an object, which wraps both the functions:
var camera = (function() {
function a() {
console.log('calling ..a');
}
function b() {
console.log('calling ..b');
}
//exposing a and b as a public API
return {a, b}
})();
//calling function a() from outside
camera.a();
camera.b();
Now you created a private scope for external so you need to create a public scope.
var camera = (function () {
function a(){
console.log('calling ..a');
}
function b(){
console.log('calling ..b');
}
return {
a,
b
}
})();
//calling function a() from outside
camera.a();
The key of that is return the public references of the function.
return {
a,
b
}
function camera(){
this.a=function (){
console.log('calling ..a');
};
this.b=function (){
console.log('calling ..b');
};
}
//calling function a() from outside
var c=new camera();
c.b();
I'm new for OOPS JavaScript. Be clear. See my JavaScript code here,
function a(){
this.first = "Kar";
}
function b(){
this.last = "Sho";
}
function c(){
this.getName = function(){
return this.first+this.last;
}
}
c.prototype.u = new a();
c.prototype.v = new b();
var d = new c();
alert(d.getName());
Here, I'm getting following output,
NaN
But I want to print KarSho. Where is problem?
I know following method,
b.prototype = new a();
c.prototype = new b();
Actually what I want is, just call a and b in c. That's it.
Call both a and b in the c constructor.
function a(){
this.first = "Kar";
}
function b(){
this.last = "Sho";
}
function c(){
a.call(this);
b.call(this);
this.getName = function(){
return this.first+this.last;
}
}
var d = new c();
alert(d.getName());
c.prototype.u = new a();
c.prototype.v = new b();
Creates instances of the a and b object on the c.v and c.u prototype properties.
To access them, you would call them by:
function c(){
this.getName = function(){
return this.v.first + this.u.last;
}
}
This is not really inheritance, but rather assigning of properties.
I have two function like this..
function a() {
function b() {
alert('reached');
}
}
how i call function b() from outside of function a()..
In general, you can't. The point of defining a function inside another function is to scope it to that function.
The only way for b to be accessible outside of a is if (when you call a) you do something inside a to make it available outside of a.
This is usually done to create a closure, so that b could access variables from a while not making those variables public. The normal way to do this is to return b.
function a() {
var c = 0;
function b() {
alert(c++);
}
return b;
}
var d = a();
d();
d();
d();
var e = a();
e();
e();
e();
You can rearange your code to this
function a() {
this.b = function() {
alert('reached');
}
}
now instantiate it and run:
c = new a();
c.b();
You will need to return the inner function call.
function a() {
function b() {
alert('reached');
}
return b();
}
And then you can call this function simply by calling function a like this:
a();
The output will be the required alert.
I have such a code:
function A() {
this.hello = function() {
console.log("I'm A");
}
}
function B() {
this.hello = function() {
// I need to call A.hello here, like parent.hello();
B.prototype.hello(); // This is wrong, TypeError
console.log("I'm B");
}
}
B.prototype = new A();
var b = new B();
b.hello();
#=> TypeError: Cannot call method 'hello' of undefined
I read some similar questions here but they all use this technique, they assign a method to a prototype.
FaqPage.prototype.init = function(name, faq) {
BasePage.prototype.init.call(this, name);
this.faq = faq;
}
FaqPage.prototype.getFaq = function() {
return this.faq;
}
But it is not in my case. My prototype is a parent's instance. How may call a parent method in my case? Or do I have to refactor my code?
You need to assign the this.hello a value, at the moment you are just creating a function to run.
Try the following :
function A() {
this.hello = function() {
console.log("I'm A");
}
}
function B() {
this.hello = function() {
B.prototype.hello(); // Now runs correctly and logs "I'm A"
console.log("I'm B");
}
}
B.prototype = new A();
var b = new B();
b.hello();
By changing the code to be this.hello = function() { } we are creating a property of the object that can be called from outside the object.
The result of calling b.hello(); is :
I'm A
I'm B
Example JSFiddle