what is difference between return module or ‘this’? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of what is difference between return module or ‘this’? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

hi! what is difference between return module or this?

return module and get instance is difference when i return this?

what is difference between const obj and this.obj in this case?

let module = function (value) {

    const obj = {};
// or
    this.obj = {};


    this.title = function (value) {
        this.obj.title = value
        return this;
    }
// or
    module.title = function (value) {
        obj.title = value
        return module;
    }

    return module;
    // or
    return this;
}

Answer

  1. return module vs return this, module is the function and this the object. That means that if you try to call the module, you will always get the function again and can end up doing calls like module("")("")("") and always get the same result. While if you only do it with return this you will get the object after calling the function module("") and then will be able to access all of the values from the object
  2. When you are doing this.title you are modifying the object, meaning that if you removed the module.title = ... part you will never be able to access title if you return the module instead of this
  3. And finally const obj is creating the variable inside the function, which means you can access it inside the function and can’t use it outside, while this.obj is creating it as a property for the object, so if you get the object by doing module("") you can access the property obj like module("").obj

Also, you can always play with the variables to see how it works, if it isn’t clear, you can copy this snippet and modify it.

let module1 = function(value) {
  const obj = {}; // can only use it inside the function
  this.obj = {}; // property of the object

  this.title = function(value) { // you're modifying the object you're inside
    this.obj.title = value
    return this; // this is different from the outside this
  }
  module1.title = function(value) { // you're modifying the variable module and not the object it returns
    obj.title = value
    return module1;
  }

  return module1;
}

console.log(module1("").obj)
console.log(module1("").title)
console.log(module1("")(""))

let module = function(value) {
  const obj = {}; // can only use it inside the function
  this.obj = {}; // property of the object

  this.title = function(value) { // you're modifying the object you're inside
    this.obj.title = value
    return this; // this is different from the outside this
  }
  module.title = function(value) { // you're modifying the variable module and not the object it returns
    obj.title = value
    return module;
  }

  // return module;
  return this;
}

console.log(module("").obj)
console.log(module("").title)
console.log(module("")(""))
We are here to answer your question about what is difference between return module or ‘this’? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji