Why is parseInt a function instead of a method?
Function:
var i = parseInt(X);
Method:
var i = X.parseInt();
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Edit:
I’m not 100% sure why
parseIntisn’t a method ofString, except that it can be run on anything. Seems it could be part ofMathbut it isn’t really a mathematical operation either.End Edit
parseIntis a method of the global object. In the browser, the global object iswindow. You could callwindow.parseInt(), but the JS engine lets you shortcut calls to global methods.That said, there is some cost to it as the engine must scan the scope chain looking for definitions of
parseInt. Generally, if I am making a single to call to such a method within a scope, I will reference it off the global:If my code needs to make more than one call to the method within a scope, however, I localize it and use the reference: