JavaScript更改原始对象valueOf的方法

时间:2015-03-19    点击:77   

本文实例讲述了JavaScript更改原始对象valueOf的方法。分享给大家供大家参考。具体分析如下:

JS中的对象都包含valueOf方法,我们可以通过自定义valueOf函数替换掉原始object的valueOf

function foo() {
  this.valueOf = function() {
   return 'this is my value';
  }
}
var bar = new foo();
Print( bar ); // prints: this is my value
Print( bar == 'this is my value' ) // prints: true
Print( bar === 'this is my value' ) // prints: false

希望本文所述对大家的javascript程序设计有所帮助。

JavaScript实现按照指定长度为数字前面补零输出的方法
JavaScript数组随机排列实现随机洗牌功能
JavaScript生成随机字符串的方法
JavaScript函数参数使用带参数名的方式赋值传入的方法
JavaScript通过字典进行字符串翻译转换的方法
> 返回     
地址:上海市普陀区胶州路941号长久商务中心 电话: QQ:
© Copyright 2012 上海网络 Product All Rights Reserved