例如,假设您有一个在节点中编写的CLI工具,只需要在传递某个标志时加载模块。
如果我在文件顶部有require('dependency')
或者我在分支机构内部有if (theFlagWasPassed) {
const dependency = require('dependency')
}
,那有什么不同吗。
@objc func didPressHorn(_ sender: UILongPressGestureRecognizer) {
let tapLocation = sender.location(in: sceneView)
if sender.state == .began {
if (wasNodeTapped(node: hornNode!, tapLocation: tapLocation)) {
hornNode!.runAction(SCNAction.playAudio(hornSound, waitForCompletion: false), forKey: "Horn")
hornPlaying = true
}
} else if sender.state == .ended {
if hornPlaying {
// tried all the possible solutions, but the sound won't stop
hornNode?.removeAction(forKey: "Horn")
hornNode?.removeAllActions()
hornNode?.isPaused = true
hornSound.volume = 0
hornPlaying = false
print("Horn off") //gets printed!
}
}
}
答案 0 :(得分:0)
是的。几乎每个程序员都会查看前几行来查找依赖项。内联导入对可读性来说是个坏主意。
还有一个性能问题(无法进行优化,但这只是猜测),这就是为什么新的import
语句必须处于最高范围。