javascript toString.call()
2015-09-17 19:19
513 查看
The
object.
Every object has a
as a text value or when an object is referred to in a manner in which a string is expected. By default, the
is inherited by every object descended from
"[object type]", where
the object type. The following code illustrates this:
Note: Starting in JavaScript 1.8.5
on
and
as defined in the 5th Edition of ECMAScript and a subsequent Errata. See Using
toString to detect object type.
You can create a function to be called in place of the default
takes no arguments and should return a string. The
want, but it will be most useful if it carries information about the object.
The following code defines the
an object of type
If you call the
The following code creates and assigns
This function generates a string containing the name, breed, color, and sex of the object, in the form "
With the preceding code in place, any time
calls the
every object, you need to call
it, passing the object you want to inspect as the first parameter called
toString()method returns a string representing
object.
Syntax
obj.toString()
Description
Every object has a toString()method that is automatically called when the object is to be represented
as a text value or when an object is referred to in a manner in which a string is expected. By default, the
toString()method
is inherited by every object descended from
Object. If this method is not overridden in a custom object,
toString()returns
"[object type]", where
typeis
the object type. The following code illustrates this:
var o = new Object(); o.toString(); // returns [object Object]
Note: Starting in JavaScript 1.8.5
toString()called
on
nullreturns
[object Null],
and
undefinedreturns
[object Undefined],
as defined in the 5th Edition of ECMAScript and a subsequent Errata. See Using
toString to detect object type.
Examples
Overriding the default toString
method
You can create a function to be called in place of the default toString()method. The
toString()method
takes no arguments and should return a string. The
toString()method you create can be any value you
want, but it will be most useful if it carries information about the object.
The following code defines the
Dogobject type and creates
theDog,
an object of type
Dog:
function Dog(name, breed, color, sex) { this.name = name; this.breed = breed; this.color = color; this.sex = sex; } theDog = new Dog('Gabby', 'Lab', 'chocolate', 'female');
If you call the
toString()method on this custom object, it returns the default value inherited from
Object:
theDog.toString(); // returns [object Object]
The following code creates and assigns
dogToString()to override the default
toString()method.
This function generates a string containing the name, breed, color, and sex of the object, in the form "
property = value;".
Dog.prototype.toString = function dogToString() { var ret = 'Dog ' + this.name + ' is a ' + this.sex + ' ' + this.color + ' ' + this.breed; return ret; }
With the preceding code in place, any time
theDogis used in a string context, JavaScript automatically
calls the
dogToString()function, which returns the following string:
"Dog Gabby is a female chocolate Lab"
Using toString()
to detect object class
toString()can be used with every object and allows you to get its class. To use the
Object.prototype.toString()with
every object, you need to call
Function.prototype.call()or
Function.prototype.apply()on
it, passing the object you want to inspect as the first parameter called
thisArg.
var toString = Object.prototype.toString; toString.call(new Date); // [object Date] toString.call(new String); // [object String] toString.call(Math); // [object Math] // Since JavaScript 1.8.5 toString.call(undefined); // [object Undefined] toString.call(null); // [object Null]
相关文章推荐
- Javascript 继承的六种方法
- Json,List,Map的数据格式详解。
- JSON的解析之net.sf.json
- web.xml中配置JSP属性
- 解决javascript加减乘除及toFixed的误差问题
- JS事件知识的一些小整理
- javascript 函数详解
- Prototype and Constructor in JavaScript
- JSON对象与字符串之间的转换
- js checkbox多选值采集
- Javascript判断空对象
- js之location对象
- JSP中getParameter()和getAttribute()的区别
- js将long日期格式转换为标准日期格式
- hammer.js教程2
- hammer.js教程1
- 在js中使用replaceAll 替换字符串
- JavaScript闭包的底层运行机制
- 用自然语言的角度理解JavaScript中的this关键字
- 例子:韩顺平JavaScript----JS乌龟抓小鸡游戏