我有以下示例:
// MyType.js
function MyType(){}
MyType.prototype.getFile = function(){
return __filename;
}
// SubType.js
var util = require('util');
function SubType(){}
util.inherits(SubType, MyType);
// test.js
var assert = require('assert'),
subtype = require('./SubType'),
myObject = new subtype();
assert.equal(myObject.getFile(), '/some/path/SubType.js'); // currently it fails, obviously
我知道__filename
不会那样工作,但是我希望有__filename
的替代后期绑定版本或同等的东西吗?
答案 0 :(得分:0)
以下代码是否返回正确的fileName?
function MyType(){
this.__fileName=__filename;
//maybe some other code you want to re use in subtype(s)
}
MyType.prototype.getFile = function(){
return this.fileName;
}
如果fileName仍然正确,我还不确定何时使用父构造函数:
function SubType(){
MyType.call(this);
}