Encounter following lines of code, but couldn’t understand it.
What is this (/…/)(this); purpose in javascript? Does it have name for this pattern?
Code as below:
//Move.js
(function(exports){
exports.Move = function(){
};
})(this);
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.
this pattern is an “Immediately Invoked Function Expresssion“. in short, it’s just a function that is executed immediately. the
thison the end is a parameter to be sent to the inner function that will be accessed asexportsin your example, we can assume that whatever
thiswas, it’s some object that has been added aMovemethod to it.some also call this pattern the “Module Pattern” in a sense that it creates a “contained environment” so that the stuff inside it is not visible to the due to a new function scope. in other words, whatever is inside sees the outside, but the outside can only see what the inside lets it see