[英]How to access a function wihtin another function when added as property?
I need to call function b from c. 我需要从c调用函数b。 I am using the following code and I receive an error.
我正在使用以下代码,但收到错误消息。 Could you tell me what is wrong here?
你能告诉我这里有什么问题吗?
var a = function() { this.b = function() { alert('b'); }; }; var c = function() { ab(); }; c();
You could create an instance of a with new
operator . 您可以使用
new
运算符创建一个实例。
var a = function() { this.b = function() { alert('b'); }; }; var c = new a; cb();
Another way is to add a property to the function and call it without a new instance. 另一种方法是向函数添加属性,然后在不使用新实例的情况下调用它。
var a = function() {}; ab = function() { alert('b'); }; var c = function() { ab(); }; c();
The b
is not visible on the a
which you used. 该
b
是不可见的a
你用哪个。 To access it, you need to create an object of a
, because you add the b
to the this
. 要访问它,你需要创建的对象
a
,因为你添加b
到this
。 It means that it will be visible on the object
of the type a
. 这意味着它将在类型为
a
的object
上可见。
var a = function() { this.b = function() { alert('b'); }; }; var c = function() { new a().b(); // on the object }; c();
Better to have b
as prototype function instead of having it inside constructor, because each time you create a new object of a
, new function instance of b
will be created. 最好是有
b
为原型函数而不是内部构造吧,因为每次创建新的对象a
,新的功能实例b
将被创建。 With prototype only one function instance will be maintained across all object instances of a
. 与原型只有一个函数实例将跨越所有对象实例保持
a
。
var a = function() {
};
a.prototype.b = function() {
alert('b');
};
Now you can access b
by creating new instance of a
现在,您可以访问
b
通过创建新实例a
(new a()).b();
You should check how the this
keyword works in javascript. 您应该检查
this
关键字在javascript中的工作方式。 In your code, this
in the a
function is bounded to the global object --which is window
in browsers, so: 在你的代码,
this
在a
函数为界,全局对象,可呈现为window
的浏览器,因此:
var a = function() {
this.b = function() {
alert('b');
};
};
a();
window.b(); //will do the alert
As other answers state, is possible to do what your trying to do just by creating an object with the a
function, either with: 正如其他答案所述,仅通过使用
a
函数创建一个对象就可以完成您尝试做的事情,或者使用:
var a = function() {
return {
b : function(){
alert('b');
}
}
};
a().b(); //alert
Or with the new
keyword: 或使用
new
关键字:
var a = function() {
this.b = function() {
alert('b');
};
};
var obj = new a();
obj.b();
However, is worth mention that you don't need to explicitly create an object to achieve this behavior. 但是,值得一提的是,您无需显式创建对象即可实现此行为。 You can also set a
b
member, which is a function, bounded to the a
function: 您还可以设置
b
成员,它是一个函数,与a
函数绑定:
var a = function() {
//some code
};
a.b = function() {
alert('b');
};
a.b();//alert
Which is more the thing you were trying to do in your question. 您要在问题中尝试做的事情更多。 You can do this due to the fact that functions in javascript are executable objects --However, to return explicitly an object and use is better than attach members to functions, imho.
您可以执行此操作,是因为javascript中的函数是可执行对象-但是,显式返回对象并使用比将成员附加到函数更好,恕我直言。
Your original function is a constructor function , so you need to use the 'new' keyword to create an instance before calling its member function, b. 您的原始函数是一个构造函数 ,因此在调用其成员函数b之前,需要使用'new'关键字创建一个实例。
var A = function() { this.b = function() { alert('b'); }; }; var c = function() { var _ = new A(); _.b(); }; c();
var a = function() {
this.b = function() {
alert('b');
};
return this;
};
var c = function() {
a().b();
};
c();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.