将js函数分配给局部变量后,为什么它们会失败? [英] Why do js functions fail when I assign them to a local variable?
问题描述
在clojurescript 1.9.89和Chrome 50.0.2661.102中,我可以创建以下日志语句:
(js / console .log作品)
但是我无法创建类似这样的文件:
(def brokenlog js / console.log)
(中断日志中,但不在这里)
-> ; #object [TypeError TypeError:非法调用]
当我尝试通过打印的值来比较这些方法时我自己的 brokenlog
函数,看起来就像是真实的函数,即 brokenlog
和 js / console.log
为我评估为:
#object [log函数log(){[native code]}]
类似地,我看到以下行为: / p>
cljs.user => (js / document.querySelector body)
#object [HTMLBodyElement [object HTMLBodyElement]]
cljs.user => (def l js / document.querySelector)
#’cljs.user / l
cljs.user => (l body)
#object [TypeError TypeError:非法调用]
nil
升级到Chrome 52可修复 console.log
行为,但不能修复 document.querySelector
行为。
所以我有两个问题:
1。我错过了什么
2。我应该在哪里阅读官方文档来解释它?
谢谢!
您正在使用哪个浏览器和clojurescript版本?
,如果在框中显示 nil ,以下代码应可在您的浏览器上使用。
.as-console-wrapper .as-console {display:none;}
< pre>< code class = language-klipse>(js / console.log工作!)(def breaklog js / console.log )(brokenlog再次工作!);在浏览器日志中应该看到两行< / code>< / pre>< script> window.klipse_settings = {选择器:'.language-klipse',//要选择klipsify的html元素的css选择器};< / script>< script src = http://app.klipse.tech/plugin /js/klipse_plugin.js?\"></script><link href = http://app.klipse.tech/css/codemirror.css rel = stylesheet />
EDIT
这里是clojurescript
(ns hello-world.core)
(def mylog js / console.log)
(mylog Hello)
已编译为javascript
hello_world.core.mylog = console.log;
hello_world.core.mylog.call(null, Hello);
console.log.call(null,....)触发器chrome漏洞console.log期望这个
是控制台
对象。如问题日志中所述 https://bugs.chromium .org / p / chromium / issues / detail?id = 167911 。
In clojurescript 1.9.89 and Chrome 50.0.2661.102, I can create a log statement like:
(js/console.log "works")
But I can't create one like:
(def brokenlog js/console.log)
(brokenlog "but not here")
--> #object[TypeError TypeError: Illegal invocation]
When I try to compare these approaches by printing the value of my own brokenlog
function, it looks just like the "real one" -- that is, both brokenlog
and js/console.log
evaluate for me as:
#object[log "function log() { [native code] }"]
Similarly, I see this behavior with:
cljs.user=> (js/document.querySelector "body")
#object[HTMLBodyElement [object HTMLBodyElement]]
cljs.user=> (def l js/document.querySelector)
#'cljs.user/l
cljs.user=> (l "body")
#object[TypeError TypeError: Illegal invocation]
nil
Upgrading to Chrome 52 fixes the console.log
behavior, but not the document.querySelector
behavior.
So I have two questions:
1. What am I missing
2. Where are the official docs I should be reading that would explain it?
Thanks!
Which browser and clojurescript version you are using? the following code should work on your browser if it display nil into box.
.as-console-wrapper .as-console {
display: none;
}
<pre><code class="language-klipse">
(js/console.log "Work!")
(def brokenlog js/console.log)
(brokenlog "Work again!")
; two line should be seen in your browser log
</code></pre>
<script>
window.klipse_settings = {
selector: '.language-klipse', // css selector for the html elements you want to klipsify
};
</script>
<script src="http://app.klipse.tech/plugin/js/klipse_plugin.js?"></script>
<link href="http://app.klipse.tech/css/codemirror.css" rel="stylesheet"/>
EDIT
Here is the clojurescript
(ns hello-world.core)
(def mylog js/console.log)
(mylog "Hello")
compiled to javascript
hello_world.core.mylog = console.log;
hello_world.core.mylog.call(null,"Hello");
console.log.call(null, ....) trigger the chrome bug, console.log expecting this
is the console
object. It should be fixed as mentioned in the issue log https://bugs.chromium.org/p/chromium/issues/detail?id=167911.
这篇关于将js函数分配给局部变量后,为什么它们会失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!