Custom methods n properties

In AS2 we can not add methods n properties to built-in classes in the way we do in AS1. Like we would -say- write the custom “isNatural” method on the “Math” class as follows :

  • The following code is valid in AS1 but not in AS2

Math.isNatural = function(number)
	   var isNotNegative = ( number>=0)
	   var isInteger = (number%1 == 0) 
	   return isNotNegative && isInteger
} 
// test
trace (Math.isNatural(7))

As the “Math class” is static internally. We know why we can not add custom methods n properties to every class, as the way we do in AS1. Later I came up with senocular’ s very nice tutorial on OOP in AS2. And now i know how we can do it in AS2 style in an efficient n organised way.

But i have a problem with the following code. It should not work ! but it does…

  • Following code is valid for both in AS1 and AS2.
 
var newMath = Math
// radian to Degree
newMath.isNatural = function(number)
		var isNotNegative = ( number>=0)
		var isInteger = (number%1 == 0) 
		return isNotNegative && isInteger
} 
// test
trace (newMath.isNatural(7))

The problem with this, is : it s workin in AS2. The Compiler refuses to recognize : “Math.isNatural” which is normal and understandable.
But after we create a variable called “newMath” n assign it the value (the class) “Math”, it does not have a problem with that . ???

Compiler looks for the variable’ s value n finds it as the “Math” class. So i think, still it should warn me with an error that " there s no property called ‘isNatural’ in Math class ".

Am i mistaken somewhere and if so where ?..