Last active
December 29, 2015 07:28
-
-
Save lili21/7635784 to your computer and use it in GitHub Desktop.
javascript good parts function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//原型继承 | |
if (typeof Object.beget !== 'function') { | |
Object.create = function(o) { | |
var F = function() {}; | |
F.prototype = o; | |
return new F(); | |
}; | |
} | |
//var new_o = Object.create(old_o); | |
//扩充功能 | |
Function.prototype.method = function(name, func) { | |
if (!this.prototype[name]) { | |
this.prototype[name] = func; | |
} | |
return this; | |
}; | |
//取整 | |
Number.method('integer', function() { | |
return Math[this < 0 ? 'ceil' : 'floor'](this); | |
}); | |
//移除首尾空白 | |
String.method('trim', function() { | |
return this.replace(/^\s+|\s+$/g, ''); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment