Why not parseInt method?

Why is parseInt a function instead of a method?

Functions:

var i = parseInt(X); 

Method:

 var i = X.parseInt(); 
+6
source share
1 answer

Edit:

I am not 100% sure why parseInt not a String method, except that it can be run on anything. It seems that it may be part of Math , but it is also not a mathematical operation.

Edit end

parseInt is a global object method. In the browser, the global window object. You can call window.parseInt() , but the JS mechanism allows you to make calls with global methods.

However, there is a certain cost to this, as the engine must scan a chain of regions looking for parseInt definitions. As a rule, if I make a single to call such a method within the scope, I will refer to it as global:

 var foo = function (someString) { var bar; // ... bar = window.parseInt(someString, 10); // ... }; 

If my code needs to make more than one method call within the scope, I localize it and use the link:

 var foo = function (someString, someOtherString) { var parseInt = window.parseInt, bar, baz; // ... bar = parseInt(someString, 10); baz = parseInt(someOtherString, 10); // ... }; 
+13
source

Source: https://habr.com/ru/post/908980/


All Articles