在node.js 绝对路径和相对路径混用的问题?

新手上路,请多包涵

疑惑:使用相对路径和绝对路径获得的是否为同一个对象??

下面为代码,

Animal类:

function Animal(name) {
    this.name = name;
}
Animal.prototype.say = function () {
  console.log(this.name+" say");
};

module.exports = Animal;

Cat类:

继承了Animal
var Animal = require('./animal');
function Cat(name) {
    Animal.call(this,name)
}
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.constructor = Cat;

module.exports = Cat;

使用相对路径时:

var Animal = require('../test/animal');
var Cat = require('../test/cat');
var cat = new Cat("judy");
console.log(cat instanceof Animal);>>>>true

使用绝对路径时:

var Animal = require('../test/animal');
var Cat = require('/webproject/node/models/test/cat');
var cat = new Cat("judy");
console.log(cat instanceof Animal);>>>>false

两种情况中,Cat都指向同一个文件,为什么其实例却不一样?

阅读 4.2k
1 个回答

然而我试了你的,两个都是true啊?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题