1

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;
}
H.jalali
  • 123
  • 5
  • `module` is the function. `this` would be some object that's unlikely to be the function. [it's hard to say what, since it's determined at call time](https://stackoverflow.com/q/3127429) – VLAZ Mar 03 '21 at 20:28
  • Using the `const` example `module.obj` will be undefined. – evolutionxbox Mar 03 '21 at 20:31

1 Answers1

2
  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("")(""))
Pietro Nadalini
  • 1,218
  • 2
  • 10
  • 22