疑惑:使用相对路径和绝对路径获得的是否为同一个对象??
下面为代码,
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都指向同一个文件,为什么其实例却不一样?
然而我试了你的,两个都是true啊?