1

let's say,

function f(){}

f();

When the function f() is defined a function object gets created in memory (along with it's prototype object)

Will the execution of the function further create any new objects? If yes, what it is ?

EDIT:

I am trying to understand the module pattern implemented in Javascript

function f(){
    var name=""; 
    out = {
      getName: function(){return name;}, 
      setName: function(newName){name = newName}
    }; 
    return out;}; 

var x= f(); 

var y = f();

Calling x.setName("foo"); does not affect the value of y.getName()

Where actually is the variable name stored for x and y if not in any object?

3
  • Constructor mode of function execution will definitely create a new object. Will the non-constructor mode of function execution i.e. f() create any new object? Commented Feb 9, 2017 at 11:31
  • should be easy to check using chrome's profiling instruments Commented Feb 9, 2017 at 11:34
  • From what I'm told, placing a () next to a function/routine is the closing of a stream, after declaring f opened it. Commented Feb 9, 2017 at 11:44

2 Answers 2

1

Your edited question is totally different from the first version.. in this case you explicitly create a new object every time you execute f. The returned object is different everytime. so if you do

var x= f(); 

var y = f();

x is different from y.

Sign up to request clarification or add additional context in comments.

2 Comments

I apologize. Now I understand, I didn't ask the right question. I will ask a new question. Thank you for the help
never mind, you're welcome! mark the answer as resolved if it is :)
1

Indeed the fonction definition will create an object with the variables in its scope and add that object to its scope chain. when executing, it invokes its scope chain an add it to the current scope (the scope chain of the function in which it is called, which already exist). So no calling a function doesn't create any object

2 Comments

I am trying to understand the module pattern implemented in Javascript .........function f(){var name=""; out = {getName: function(){return name;}, setName: function(newName){name = newName}}; return out;}; var x= f(); var y = f(); ..............Calling x.setName("foo"); does not affect the value of y.getName() ..... where actually is the variable name stored for x and y if not in any object
I am trying to understand the module pattern implemented in Javascript function f(){ var name=""; out = {getName: function(){return name;}, setName: function(newName){name = newName}}; return out;}; var x= f(); var y = f(); Calling x.setName("foo"); does not affect the value of y.getName() ..... where actually is the variable name stored for x and y if not in any object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.