在将打字稿编译成javascript时,如何避免创建全局函数。
好处:混淆器不必提供公共API
Foo.ts:
db.orders.aggregate([
{
$unwind: "$orderedItems" // flatten the "orderedItems" array
},
{
$group: {
"_id": "$orderedItems.name", // group by orderItems.name
"total": {
$sum: "$orderedItems.qty" // calculate total quantity
},
"users": {
$addToSet: "$client" // create an array of clients who have ordered each item
}
}
}, {
$lookup: { // retrieve the stock information
from: "stock",
localField: "_id",
foreignField: "item",
as: "stock"
}
}, {
$unwind: "$stock" // flatten the result array - should always return only one document, given your data model
}, {
$addFields: { // add a new field...
"difference": { // ...called "difference"...
$subtract: [ "$stock.qty", "$total" ] // ...in which we store the difference between the stock qty and the ordered qty
}
}
}, {
$match: { // only return documents...
"difference": { // ...where the value for "difference"...
$lt: 0 // ...is less than zero
}
}
}])
Foo.js:
class Foo {}
Foo.obfuscated.js(使用npm jsobfuscator):
var Foo = (function () {
function Foo() {
}
return Foo;
}());
Foo仍然可见。我理解为什么(公共API)。解决方案是:
Foo.isolated.js:
var _0xcd14=[];var _0x12e2=[];var Foo=(function(){function Foo(){}return Foo}())
Foo.isolated.obfuscated.js(我想要的):
(function() { /* code from Foo.js */ })();
是否有var _0xe1f1=[];var _0xa6a8=[];(function(){var _0x64f8x2=(function(){function _0x64f8x2(){}return _0x64f8x2}())})()
/编译器选项的打字稿设置,如tsconfig.js
或其他什么?
答案 0 :(得分:1)
在函数范围内包装类应该可以解决问题。
(function(){
class Foo {}
})
据我所知,没有选项形式的内置支持。让我知道如果做上述事情无法实现您的目标 - 在这种情况下,有关目标确切内容的更多详细信息将有助于我们了解您的目标。
答案 1 :(得分:0)
嵌套(私有)类?
module MyModule {
export class MyPublicClass {
private myPrivateClass: PrivateClass;
constructor() {
this.myPrivateClass = new PrivateClass;
}
public test() {
this.myPrivateClass.test();
}
}
class PrivateClass {
public test() {
console.log('it works');
}
}
}