Go与JavaScript中方法的执行上下文
下面是两个代码片段——一个在Go中,另一个在JavaScript中——基本上做着相同的事情 //开始Go与JavaScript中方法的执行上下文,javascript,go,Javascript,Go,下面是两个代码片段——一个在Go中,另一个在JavaScript中——基本上做着相同的事情 //开始 package main import "fmt" type Engine struct { bootTimeInSecs int } func (e *Engine) Start() { fmt.Printf("Engine starting in %s seconds ...", e.bootTimeInSecs) } type Start func() type
package main
import "fmt"
type Engine struct {
bootTimeInSecs int
}
func (e *Engine) Start() {
fmt.Printf("Engine starting in %s seconds ...", e.bootTimeInSecs)
}
type Start func()
type BenchmarkSuite struct {
workloads []string
start Start
}
func main() {
engine := Engine{10}
benchmarkSuite := BenchmarkSuite{workloads: []string{}, start: engine.Start}
benchmarkSuite.start()
}
输出
Engine starting in 10 seconds ...
function Engine(bootTimeInSecs) {
this.bootTimeInSecs = bootTimeInSecs
}
Engine.prototype.constructor = Engine
Engine.prototype.Start = function() {
console.log("Engine starting in " + this.bootTimeInSecs + " seconds ...")
}
function BenchmarkSuite(workloads, start) {
this.workloads = workloads
this.start = start
}
BenchmarkSuite.prototype.constructor = BenchmarkSuite
engine = new Engine(10)
benchmarkSuite = new BenchmarkSuite([], engine.Start)
benchmarkSuite.start()
Engine starting in undefined seconds ...
//JavaScript
Engine starting in 10 seconds ...
function Engine(bootTimeInSecs) {
this.bootTimeInSecs = bootTimeInSecs
}
Engine.prototype.constructor = Engine
Engine.prototype.Start = function() {
console.log("Engine starting in " + this.bootTimeInSecs + " seconds ...")
}
function BenchmarkSuite(workloads, start) {
this.workloads = workloads
this.start = start
}
BenchmarkSuite.prototype.constructor = BenchmarkSuite
engine = new Engine(10)
benchmarkSuite = new BenchmarkSuite([], engine.Start)
benchmarkSuite.start()
Engine starting in undefined seconds ...
输出
Engine starting in 10 seconds ...
function Engine(bootTimeInSecs) {
this.bootTimeInSecs = bootTimeInSecs
}
Engine.prototype.constructor = Engine
Engine.prototype.Start = function() {
console.log("Engine starting in " + this.bootTimeInSecs + " seconds ...")
}
function BenchmarkSuite(workloads, start) {
this.workloads = workloads
this.start = start
}
BenchmarkSuite.prototype.constructor = BenchmarkSuite
engine = new Engine(10)
benchmarkSuite = new BenchmarkSuite([], engine.Start)
benchmarkSuite.start()
Engine starting in undefined seconds ...
我知道JavaScript的解决方法,但这不是问题所在。为什么JavaScript决定不保留函数的原始执行上下文?在JavaScript中,当您将对象传递给
基准测试套件
构造函数时,函数未绑定到对象引擎
必须将对象显式绑定到函数。
这就是你必须要做的
benchmarkSuite = new BenchmarkSuite([], engine.Start.bind(engine))
bind()最简单的用法是生成一个函数,该函数无论如何调用,都是使用特定的this值调用的。对于新的JavaScript程序员来说,一个常见的错误是从对象中提取一个方法,然后调用该函数,并期望它使用原始对象作为它的this(例如,在基于回调的代码中使用该方法)。然而,如果不特别小心,原始对象通常会丢失。使用原始对象从函数创建绑定函数,巧妙地解决了这个问题
你可以参考更多
函数引擎(boottimenses){
this.boottimenses=boottimenses
}
Engine.prototype.constructor=引擎
Engine.prototype.Start=函数(){
console.log(“引擎在“+this.boottimenses+”秒内启动…”)
}
功能基准测试套件(工作负载、启动){
this.workloads=工作负载
this.start=开始
}
BenchmarkSuite.prototype.constructor=BenchmarkSuite
发动机=新发动机(10)
benchmarkSuite=新的benchmarkSuite([],engine.Start.bind(engine))
benchmarkSuite.start()
无论是谁对该问题进行了结束
投票,您能否解释该问题如何吸引基于意见的答案?请检查答案,您忘记绑定function@bharat-如果答案对你有帮助,请检查并投票:D