一道js面试题


var write = document.write;
write("hello");
//能够正确执行吗?
//如果不正确,正确操作是怎样的?

JavaScript

青铜5的想法 9 years, 7 months ago
Glamic answered 9 years, 7 months ago

不能正确执行,因为 write 函数丢掉了上下文。
正确的操作应该是:


 1: var write = document.write.bind(document)

或者


 2: write.call(document, "hello")

AEGIS answered 9 years, 7 months ago

Your Answer