zoukankan      html  css  js  c++  java
  • Function.prototype.toString

    语法:fn.toString(indentation)

    改方法返回当前函数源代码的字符串,而且还可对此字符串进行操作,比如:

    function num(){ };
    var str = num.toString();
    console.log(str);//"function num(){}"
    
    console.log(typeof str); //string类型
    
    var arr = str.split("{");
    console.log(arr);//["function num()","}"]

    如果是内置函数使用了此方法的话,往往会返回类似于"[native code]"作为函数体的函数,比如:

    var str = Function.prototype.bind.toString();
    console.log(str);//function bind() { [native code] }

    使用情况大概是这样,但是还需要知道的是任何函数使用toString方法都是继承于Funtion.prototype而不是Object.prototype。

    我可以用原型链来解释一下:fn--__proto__-->Function.prototype --__proto__-->Object.prototype

                           (有toString方法)                          (有toString方法)

    可以看出:在这一条原型链中,Function.prototype的toString方法把Object.prototype的覆盖了!

  • 相关阅读:
    iOS 日历控件
    iOS签发者无效
    Swift3
    GitHub管理代码-随笔
    iOS10 适配问题-Xcode8
    iOS 性能调试
    bug
    贝塞尔曲线(UIBezierPath)属性、方法汇总
    webView 自适应高度 document.body 属性
    iOS-集成支付宝支付、微信支付简单总结
  • 原文地址:https://www.cnblogs.com/wang-jiang/p/4458563.html
Copyright © 2011-2022 走看看