Просмотр полной версии : Изменить Math.max
Объясните пожалуйтса, как сделать так чтобы этот код заработал. Мне нужно чтобы, Math.max сравнивал три числа!
спасибо
mathPlus = new Object();
mathPlus.__proto__ = Math;
mathPlus.oldMax = Math.max;
mathPlus.max = function(x, y, z) {
val1 = mathPlus.oldMax(x, y);
val2 = mathPlus.oldMax(val1, z);
return val2;
};
Object.prototype.Math = mathPlus;
Math.max(4, 5, 6);
Techmaker
06.11.2003, 02:59
newMax=function(){
for(count=0,maxNum=false;count<arguments.length;count++){
maxNum=Math.min(maxNum,arguments[count])
}
for(count=0;count<arguments.length;count++){
maxNum=Math.max(maxNum,arguments[count])
}
return(maxNum)
}
trace(newMax(4,15,8,20,50,1))
trace(newMax(-4,-1))
//by Techmaker (newtech@front.ru)
возвращает максимальное число из перечисленных чисел(количество чисел неограничено).
:) ;)
Techmaker
06.11.2003, 13:40
вот так поэкономичнее будет :)
newMax=function(){
for(maxNum=arguments[0],count=1;count<arguments.length;count++){
maxNum=Math.max(maxNum,arguments[count])
}
return(maxNum)
}
trace(newMax(4,15,8,20,50,1))
trace(newMax(0,-4,-1))
//by Techmaker (newtech@front.ru)
Пушистик
06.11.2003, 19:18
а у меня другая функция:
function getMax() {
function sortUp(el1, el2) {
return el1-el2;
}
arguments.sort(sortUp);
last = arguments.length-1;
return arguments[last];
}
пример использования:
trace(getMax(220, 54, 8, 71, 2));
trace(getMax(4,6,5));
--------------------------
Пушистик
06.11.2003, 19:22
нееее.....вот так вот буит поэкономинее :D
function getMax() {
function sortUp(el1, el2) {
return el2-el1;
}
arguments.sort(sortUp);
return arguments[0];
}
Я думаю, что я смогу сделать свою собственную функцию! Хотя спасибо вам за примеры. Мой вопрос был следующий: как переделать уже существующий метод в ActionScript?
Вот вам пример, который работает, кстати говоря в данном примере Math.random будет брать числа между 24 и 60;
mathTest = new Object();
mathTest.__proto__ = Math;
mathTest.oldRand = Math.random;
mathTest.random = function(min, max) {
return Math.floor(mathTest.oldRand()*((max-min)+1))+min;
};
Object.prototype.Math = mathTest;
for (i=0; i<20; i++) {
a = Math.oldRand();
b = Math.random(24, 60);
trace("oldRand: "+a+" Random: "+b);
}
Вот оно! А почему Math.max не работает(самое первое сообщение)?
Всем огромное спасибо!
Techmaker
08.11.2003, 15:08
2lehin
а зачам тебе извращатся и заменять базовую функцию :)
Работает на vBulletin ® версия 3.7.3. Copyright ©2000-2026, Jelsoft Enterprises Ltd. Перевод: zCarot
Copyright © 1999-2008 Flasher.ru. All rights reserved.