如果我通过new constructor()
电话来制作一个对象,那就是:
function constructor(){
function privateFn(){
console.log('this is private');
}
this.publicFn = function(){
console.log('this is public');
}
function doSomething(){
privateFn();
publicFn();
}
console.log(this.publicFn ? "exists" : "does not exist"); // logs exists
doSomething(); //throws an error -> publicFn() is not this.publicFn().
}
new constructor();
所以问题是,有没有办法在没有this.
部分的情况下访问它?
即使没有this.
,我的IDE(netbeans)似乎也能识别它,虽然这并不一定意味着什么,但它让我想知道,是否有可能以某种方式引用publicFn()
作为函数,而不是对象的属性?也许以不同的方式构建它?
编辑:目标是创建一个具有私有和公共方法的对象,使用new
构造函数,但同时允许构造函数本身以及所有对象的方法调用没有this.
前缀的公共方法。
BEKIM BACAJ的特别编辑
专门为您更新此部分只是为了向您展示我的意思。
这不是console.log欺骗我,在确实创建对象之后调用doSomething
或其他任何方法。它仍然无法访问,现在我已经看到了为什么这样的答案,这是有道理的。私有方法的上下文this
不一样,或者构造函数中的函数将this
设置为window,因此它们的上下文this
与公共的上下文不同function constructor(){
function privateFn(){
console.log('Call to privateFN succesful');
console.log('------------------------------------------------------------------------------------');
}
function doSomething(){
console.log('Executing: doSomething;');
//I want to be able to access both private and public methods here (which i can)
//I want to be able to access them the same way (which I cannot).
privateFn(); //works
publicFn(); //error
//Read NOTE1 below.
}
this.callDoSomething = function(){
console.log('Call to callDoSomething succesful');
console.log('------------------------------------------------------------------------------------');
//made just so that you can access doSomething from the outside to test.
doSomething();
};
this.publicFn = function(){
console.log('Call to publicFN succesful');
console.log('------------------------------------------------------------------------------------');
};
//.... many more methods (we are talking hundreds)
//.... some of them are public, some are private
//.... some are resutls of calling functions that return functions from other files (collaborative work),
//.... these might be public or private, which we achive like so:
//.... var externalModule = extrenalModuleInitializer(this);
//.... as you can imagine this externalModuleInitializer function can either:
//.... A) PRIVATE: return a function, making this externalModule variable a private method
//.... B) PUBLIC: do stuff with the `this` object that was passed, adding public methods to it
//.... So since I do not know if certain parts of this object will be private or public, i need
//.... a way to access them in the same way in order to make the coding easier.
}
console.clear();
var a = new constructor();
a.publicFn();
a.callDoSomething();
他们试图打电话的方法。 Phylogenesis建议的模式是我正在寻找的,它符合需要。
.about-text {
padding: 5% 5% 0 5%;
}
#gas-safe a {
color: #4E6E9B;
border-bottom: 1px dotted #4E6E9B;
}
#to-top {
display: block;
text-align: center;
text-decoration: none;
color: #4E6E9B;
}
#to-top i {
font-size: 50px;
text-align: center;
}
#to-top:hover {
color: #fff;
}
答案 0 :(得分:3)
我认为你真正追求的是module pattern:
function MyConstructor() {
var publicFn = function () {
console.log('this is public');
}
var privateFn = function () {
console.log('this is private');
}
var doSomething = function () {
privateFn();
publicFn();
}
doSomething();
return {
publicFn: publicFn
};
}
请注意您如何返回“公开”可访问方法的列表,但内部没有任何内容引用this
。
这方面的一个例子是here。
答案 1 :(得分:0)
我不确定它是否符合你的需要,但是这个(最后的return
语句)公开了这个方法:
function constructor(){
function privateFn(){
console.log('this is private');
}
this.publicFn = function(){
console.log('this is public');
}
function doSomething(){
privateFn();
publicFn();
}
return{
publicFn: this.publicFn
}
}
你可以这样称呼它:
var constr = constructor();
constr.publicFn();
答案 2 :(得分:0)
this
是根据您的函数描述的原型对要创建的对象(但尚不存在)的引用,因此在声明<时,您无法避免使用this
关键字em> public 方法。从技术上讲,您可以在构造函数外部定义此函数,如下所示:
function publicFn(){}
function constructor() {
....
}
var obj = new constructor();
obj.publicFn = publicFn;
但是,我很害怕,它没有多大意义
答案 3 :(得分:0)
为了设计这个响应,我依靠&#34;使用自我调用功能和调用&#34;由Yves M.in提供discussion
var Constructor = (function (){
function Constructor (){}
function privateFn(){
console.log('this is private');
}
Constructor.prototype.publicFn = function(){
console.log('this is public');
}
Constructor.prototype.doSomething = function(){
return privateFn.call(this);
}
return Constructor;
})();
var Constructor = new Constructor();
Constructor.publicFn();
Constructor.doSomething();
&#13;
注意:即使空构造函数方法似乎什么都不做,但运行的代码必须没有错误。否则,您会看到一条错误消息,指出:&#34;无法读取属性&#39;原型&#39;未定义&#34;,
隐藏privateFn()方法的原因是它不是Constuctor对象原型的一部分。此外,我能够在不提供此参数的情况下使privateFn的调用方法正常工作,但它是call方法签名的一部分。根据文档,忽略提供 this 会导致null和undefined参数被&#34; ...替换为全局对象,原始值将被转换为对象。&#34;
答案 4 :(得分:0)
没有this
的构造函数中的方法无法公开。如果要使用位于构造函数内的方法,则需要使用this
function Constructor(){
function privateFn() {
console.log('this is private');
}
function publicFn () {
console.log('this is public');
}
this.doSomething(){
privateFn();
publicFn();
}
}
let obj = new Constructor();
obj.doSomething();
但是如果你想在构造函数中调用方法,你可以这样做:
function Constructor(){
let self = this;
function privateFn() {
console.log('this is private');
}
this.publicFn = function () {
console.log('this is public');
}
function doSomething(){
privateFn();
self.publicFn();
}
doSomething()
}