【发布时间】:2017-10-26 11:39:05
【问题描述】:
在 Ruby 中,我可以在运行时扩展对象上的模块。我认为 JavaScript 可以获取该功能,但我无法让它工作。
Ruby 运行正常,对象有test1 和test2 方法:
class Test
def test1
puts "test1"
end
end
module Other
def test2
puts "test2"
end
end
test = Test.new
test.extend(Other)
test.test1
test.test2
JavaScript 返回一个 TypeError: test_new.test2 is not a function
class Test {
test1(){
console.log("test1")
}
}
class Other {
test2() {
console.log("test2")
}
}
console.log(Object.getOwnPropertyNames( Test.prototype ))
console.log(Object.getOwnPropertyNames( Other.prototype ))
var test = new Test
var test_new = Object.assign(test, Other.prototype)
test_new.test1()
test_new.test2()
有谁知道我如何获得它?
【问题讨论】:
-
@FelixKling,是的,我发现它似乎导致“原型链上的属性和不可枚举的属性无法复制”,谢谢。
标签: javascript ruby-on-rails ruby node.js