在以下示例中,我如何直接从$("#guessButton").click(function(e) {
e.preventDefault();
counter++;
$('#count').text(counter);
var guess = $('#userGuess').val();
if (guess == number) {
$('#feedback').text("You've guessed the number!!");
$('#guessList').append("<li>"+guess+"</li>");
} else if (Math.abs(number-guess) <= 50 && Math.abs(number-guess) >= 30) {
$('#feedback').text("Cold");
$('#guessList').append("<li>"+guess+"</li>");
} else if (Math.abs(number-guess) <= 30 && Math.abs(number-guess) >= 20) {
$('#feedback').text("Warm");
$('#guessList').append("<li>"+guess+"</li>");
} else if (Math.abs(number-guess) <= 20 && Math.abs(number-guess) >= 10) {
$('#feedback').text("Hot");
$('#guessList').append("<li>"+guess+"</li>");
} else if (Math.abs(number-guess) <= 10 && Math.abs(number-guess) >= 1) {
$('#feedback').text("Very Hot");
$('#guessList').append("<li>"+guess+"</li>");
} else if (Math.abs(number-guess) > 50) {
$('#feedback').text("Ice Cold");
$('#guessList').append("<li>"+guess+"</li>");
}
});
致电Base.foo()
,我理解为什么它首先调用Base.bar()
,但有没有办法阻止这种情况。
Test.foo()
输出将是:
class Base {
constructor() {
console.log('Base constructor')
}
foo() {
console.log('Base foo')
}
bar() {
console.log('Base bar')
this.foo();
}
}
class Test extends Base {
foo() {
console.log('Test foo');
super.foo();
}
bar() {
console.log('Test bar');
super.bar();
}
}
const test = new Test();
test.bar();
我的预期输出是:
Base constructor
Test bar
Base bar
Test foo
Base foo
答案 0 :(得分:2)
使用ES2015课程时,您可以使用标准JavaScript功能。在这种情况下,只需使用Function.prototype.call()
:
'use strict'
class Base {
constructor() {
console.log('Base constructor')
}
foo() {
console.log('Base foo')
}
bar() {
console.log('Base bar')
Base.prototype.foo.call(this);
}
}
class Test extends Base {
foo() {
console.log('Test foo');
super.foo();
}
bar() {
console.log('Test bar');
super.bar();
}
}
const test = new Test();
test.bar();
&#13;