Function.prototype.toString 的使用技巧】的更多相关文章

Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello") } console.log( hello.toString() ); 输出: 'function hello( msg ){ \ console.log("hello") \ }' 这个方法真是碉堡了-, 通过合适的正则, 我们可以从中提取出丰富的信息. 函数名 函数形参列表…
从 ES1 到 ES5 的这 14 年时间里,Function.prototype.toString 的规范一字未变: An implementation-dependent representation of the function is returned. This representation has the syntax of a FunctionDeclaration. Note in particular that the use and placement of white sp…
语法: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);//["f…
Object.prototype 方法: hasOwnProperty 概念:用来判断一个对象中的某一个属性是否是自己提供的(主要是判断属性是原型继承还是自己提供的) 语法:对象.hasOwnProperty('属性名') var o = { name: 'jim' }; function Person() { this.age = 19; this.address='北京'; this.work='上海'; } Person.prototype = o; var p = new Person(…
Object.prototype.toString方法返回对象的类型字符串,因此可以用来判断一个值的类型. 调用方法: Object.prototype.toString.call(value) 不同数据类型的Object.prototype.toString方法返回值如下. 数值:返回[object Number]. 字符串:返回[object String]. 布尔值:返回[object Boolean]. undefined:返回[object Undefined]. null:返回[ob…
方法: hasOwnProperty isPrototypeOf propertyIsEnumerable hasOwnProperty 该方法用来判断一个对象中的某一个属性是否是自己提供的( 住要用在判断属性是原型继承的还是自己提供的 ) 语法: 对象.hasOwnProperty( '属性名' ) -> boolean isPrototypeOf 凡是看到 of 翻译成 的, 反过来翻译: prototype of obj, 翻译成 obj 的 原型 因此该方法的含义是: xxx 是 xxx…
/** * * @authors Your Name (you@example.org) * @date 2016-11-18 09:31:23 * @version $Id$ */instanceof: 1.左操作数是一个对象,右操作数是标识对象的类,如果左侧的对象是右侧类的实例,则表达式返回true,否则返回false 2.如果左边的操作数不是对象,返回false 3.如果右边的操作数不是函数,抛出类型错误异常 4.在计算obj instanceof f 时, 会先计算f.prototype…
var num = 1;alert(Object.prototype.toString.call(num)); // [object Number]var str = 'hudidit.com';alert(Object.prototype.toString.call(str)); // [object String]var boo = true;alert(Object.prototype.toString.call(boo)); // [object Boolean]var fun = fu…
判断一个对象的类型: /** * 判断对象是否为数组 * @param {Object} source 待判断的对象 * @return {Boolean} true|false */ Object.isArray = function (source) { return '[object Array]' == Object.prototype.toString.call(source); }; 测试: Object.isArray = function (source) { return '[…
一.toString()是一个怎样的方法?它是能将某一个值转化为字符串的方法.然而它是如何将一个值从一种类型转化为字符串类型的呢? 通过下面几个例子,我们便能获得答案: 1.将boolean类型的值转化为string类型: console.log(true.toString());//"true" console.log(false.toString());//"false" 2.将string类型按其字面量形式输出: var str = "test123…