在Javascript中重写函数
可以在JavaScript中重写函数吗?例如,在python中,我可以在一个文件中执行此操作:在Javascript中重写函数,javascript,python,Javascript,Python,可以在JavaScript中重写函数吗?例如,在python中,我可以在一个文件中执行此操作: #file one.py class Test: def say(self,word): pass def speak(self): self.say("hello") 然后在另一个文件中执行以下操作: import one class Override(one.Test): def say(self,word): print(
#file one.py
class Test:
def say(self,word):
pass
def speak(self):
self.say("hello")
然后在另一个文件中执行以下操作:
import one
class Override(one.Test):
def say(self,word):
print(word)
if __name__ == "__main__":
Override().speak()
由于覆盖,这可能会打印(“hello”)而不是传递
有JavaScript等价物吗 函数测试(){}
Test.prototype.say=函数(word){
警惕(word);
}
Test.prototype.speak=函数(){
这个。说‘你好’;
}
Test.prototype.say=函数(word){
console.log(word);
}
最后一个赋值将覆盖所有测试对象上的say方法。如果要在继承函数(类)中重写它:
函数测试(){}
Test.prototype.say=函数(word){
警惕(word);
}
Test.prototype.speak=函数(){
这个。说‘你好’;
}
函数TestDerived(){}
TestDerived.prototype=新测试();//javascript最简单的继承形式。
TestDerived.prototype.say=函数(word){
console.log(word);
}
如果要在测试的特定实例中重写它:
函数测试(){}
Test.prototype.say=函数(word){
警惕(word);
}
Test.prototype.speak=函数(){
这个。说‘你好’;
}
var myTest=新测试();
myTest.say=函数(word){
console.log(word);
}
函数测试(){}
Test.prototype.say=函数(word){
警惕(word);
}
Test.prototype.speak=函数(){
这个。说‘你好’;
}
Test.prototype.say=函数(word){
console.log(word);
}
最后一个赋值将覆盖所有测试对象上的say方法。如果要在继承函数(类)中重写它:
函数测试(){}
Test.prototype.say=函数(word){
警惕(word);
}
Test.prototype.speak=函数(){
这个。说‘你好’;
}
函数TestDerived(){}
TestDerived.prototype=新测试();//javascript最简单的继承形式。
TestDerived.prototype.say=函数(word){
console.log(word);
}
如果要在测试的特定实例中重写它:
函数测试(){}
Test.prototype.say=函数(word){
警惕(word);
}
Test.prototype.speak=函数(){
这个。说‘你好’;
}
var myTest=新测试();
myTest.say=函数(word){
console.log(word);
}
你能写一个JavaScript示例吗?你是说继承吗?Javascript的继承很奇怪(它是典型的,而不是经典的),但是如果你在Javascript中查找继承,你会发现如何做到这一点。你能写一个Javascript示例吗?你是说继承吗?Javascript的继承很奇怪(它是典型的,而不是经典的),但如果您在Javascript中查找继承,您应该会发现如何做到这一点。