What does it mean when a javascript function is declared in the following way:
JSON.stringify = JSON.stringify || function (obj)
{
//stuff
};
How is the above different from just declaring it like below?
function stringify(obj)
{
//stuff
}
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.
function stringifywill declare the function in the global scope (if you’re not already inside another scope, such as another function or a hash) or the scope you’re currently in.Example:
JSON.stringify = functionwill define the function on theJSONobject.Example:
JSON.stringify || functionwill only define it if it was not previously defined.Example: