Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何创建一个Javascript函数,该函数基于属性名检查对象/函数,这些属性名将在精简后仍然有效?_Javascript_Minify - Fatal编程技术网

如何创建一个Javascript函数,该函数基于属性名检查对象/函数,这些属性名将在精简后仍然有效?

如何创建一个Javascript函数,该函数基于属性名检查对象/函数,这些属性名将在精简后仍然有效?,javascript,minify,Javascript,Minify,假设我有一个函数proxyThrough,如下所示: function proxyThrough(parentClass, childObjPropertyName, methodName) { parentClass.prototype[methodName] = function() { this[childObjPropertyName][methodName].apply(this[childObjPropertyName], arguments); };

假设我有一个函数
proxyThrough
,如下所示:

function proxyThrough(parentClass, childObjPropertyName, methodName) {
    parentClass.prototype[methodName] = function() {
        this[childObjPropertyName][methodName].apply(this[childObjPropertyName], arguments);
    };
}
childPropertyName
methodName
都是字符串,它按名称查找函数。 我知道这将无法在缩小后继续存在

我怎样才能让这样的函数在缩小后生存


示例

这就是我目前正在做的:

var BaseView = require('./BaseView');
var FooView = require('./FooView');

function BarView() {
    this._fooView = new FooView();
}

BarView.prototype = Object.create(BaseView.prototype);
BarView.prototype.constructor = BarView;

BarView.prototype.anAction = function() {
    this._barView.anAction.apply(this._barView, arguments);
};

BarView.prototype.anotherAction = function() {
    this._barView.anotherAction.apply(this._barView, arguments);
};
这就是我想做的:

var BaseView = require('./BaseView');
var FooView = require('./FooView');

function BarView() {
    this._fooView = new FooView();
}

BarView.prototype = Object.create(BaseView.prototype);
BarView.prototype.constructor = BarView;

function proxyThrough(parentClass, childObjPropertyName, methodName) {
    parentClass.prototype[methodName] = function() {
        this[childObjPropertyName][methodName].apply(this[childObjPropertyName], arguments);
    };
}

['anAction', 'anotherAction'].forEach(proxyThrough.bind(null, BarView, '_fooView'));

我想这取决于minifier的工作方式,但如果它一致地重命名相同的属性名称,则可以使用helper函数来获取缩小的属性名称:

function minifiedName(obj) {
    for (var prop in obj) {
        return prop;
    }
}

[
   minifiedName({anAction: null}),
   minifiedName({anotherAction: null})
].forEach(proxyThrough.bind(null, BarView, '_fooView'));

变量/参数名称是否随着值的使用而改变,而不是名称,这无关紧要。只有当操作类似于时,才需要避免混淆。当需要将变量名与字符串值匹配时,缩小需要特殊的解决方法。变量名本身没有危险。在本任务的另一个阅读中:使用一个可以控制的模糊器,这样待代理的方法/属性(以及这些方法/属性的名称)就不会改变。一旦他们改变了。。它们已被更改(甚至在代码运行之前),并且这些名称无法解析为原始名称,除非存在保留的映射。@PM77-1和user2864740:我不太清楚您的意思。为了明确起见,我加入了一个SSCCE来说明我的用例。它看起来不会在缩小后继续存在,因为我确实将字符串传递给
proxyThrough
函数。您确定它不会在缩小后继续存在吗?虽然函数参数经常更改,但由于括号表示法的通用性,对象字段通常不会被缩微器触及。