4 ответов:
var origParseFloat = parseFloat; parseFloat = function(str) { alert("And I'm in your floats!"); return origParseFloat(str); }
Вы можете заменить любую встроенную функцию, просто объявив ее.
parseFloat = function(a){ alert(a) };теперь
parseFloat(3)будет предупреждать 3.
вы можете сделать это так:
alert(parseFloat("1.1531531414")); // alerts the float parseFloat = function(input) { return 1; }; alert(parseFloat("1.1531531414")); // alerts '1'Проверьте рабочий пример здесь:http://jsfiddle.net/LtjzW/1/
вы можете переопределить его или предпочтительно расширения это реализация, как это
parseFloat = (function(_super) { return function() { // Extend it to log the value for example that is passed console.log(arguments[0]); // Or override it by always subtracting 1 for example arguments[0] = arguments[0] - 1; return _super.apply(this, arguments); }; })(parseFloat);и назовите его так, как вы обычно называете его:
var result = parseFloat(1.345); // It should log the value 1.345 but get the value 0.345
Comments