V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
wwjvtwoex
V2EX  ›  JavaScript

JS native 函数重命名 和 代码行定位

  •  
  •   wwjvtwoex · 2018-02-27 12:24:29 +08:00 · 2787 次点击
    这是一个创建于 2221 天前的主题,其中的信息可能已经有所发展或是发生改变。

    今天遇到的问题,求高手指点,谢谢

    <1 native 函数重命名>

    定义 var p = console.log

    可以正常调用 p('x') => 输出 x

    ....................................

    但定义 var qa = document.querySelectorAll

    调用 qa('div') 发生错误 Illegal invocation

    ===========================

    <2 代码定位>

    定义函数

    <1> function p(x) {

    <2> console.log(x)

    <3> }

    然后在某处调用

    <10> p(111)

    在 chrome Console 中 只显示定义处的行号"2",不显示调用处的行号"10"

    5 条回复    2018-02-27 13:54:51 +08:00
    wwjvtwoex
        1
    wwjvtwoex  
    OP
       2018-02-27 12:46:39 +08:00
    补充 :

    var qa = document.querySelectorAll

    问题 1 的 错误信息 是 chrome 显示的

    在 firefox 下显示

    TypeError: 'querySelectorAll' called on an object that does not implement interface Document.
    sunjourney
        2
    sunjourney  
       2018-02-27 12:54:28 +08:00   ❤️ 2
    ```
    var p1 = function (...args) {
    console.log(...args)
    }

    var p2 = console.log.bind(console)

    var qa1 = function (...args) {
    return document.querySelectorAll(...args)
    }

    var qa2 = document.querySelectorAll.bind(document)
    ```
    如果不知道为什么,js 可以重修一下
    dtysky
        3
    dtysky  
       2018-02-27 13:32:16 +08:00 via Android
    func 不属于 obj,你存只是存了个引用,func 的 this 是调用时注入的。
    lz 确实应该补补 js 基础……
    DOLLOR
        4
    DOLLOR  
       2018-02-27 13:37:25 +08:00
    百度“ js 函数的四种调用方式”
    wwjvtwoex
        5
    wwjvtwoex  
    OP
       2018-02-27 13:54:51 +08:00
    谢谢大家,正在回炉中
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   939 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:50 · PVG 04:50 · LAX 13:50 · JFK 16:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.