看别人源码碰到的,做个备忘。
//针对对象增加get\set方法,返回绑定函数的返回值 Date.prototype.__defineGetter__(‘getYear‘, function() {return this.getFullYear();}); Date.prototype.__defineSetter__(‘setYear‘, function(year) {this.setFullYear(year)}); Date.prototype.__defineGetter__(‘sayHello‘, function() {return "Hello"}); var now = new Date; console.log(now.getYear); //2015 now.setYear = 2011; console.log(now.getYear); //2006 console.log(now.sayHello);
时间: 2024-10-19 00:25:58