Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8488393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:31:23+00:00 2026-06-10T21:31:23+00:00

These are excerpts from Nicholas Zakas book which im not clear about, please help

  • 0

These are excerpts from Nicholas Zakas book which im not clear about, please help me understand.

function setName(obj) {
obj.name = “Nicholas”;
obj = new Object();
obj.name = “Greg”;
}
var person = new Object();
setName(person);
alert(person.name); //”Nicholas”

The local object obj is dead in the heap after function exits ( same when it returns the obj too? )

person and obj are copies or reference to each other as an single object in heap ?

he says “When obj is overwritten inside the function, it becomes a pointer
to a local object. That local object is destroyed as soon as the function finishes executing” .

Then , in he explains factory pattern as following :

function createPerson(name, age, job){
    var o = new Object();
    o.name = name;
    o.age = age;
    o.job = job;
    o.sayName = function(){
        alert(this.name);
    };    
    return o;

since now o is local object ,and he said ” That local object is destroyed as soon as the function finishes executing” . so when functions returns o which is a local object and if i

var foo = createPerson(bla,18,student); 

then foo is a reference to a local object which must be dead by then . Please clarify the concept of “returning local objects in functions “

Sorry for the long post , mods please edit and condense if needed .

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:31:24+00:00Added an answer on June 10, 2026 at 9:31 pm

    When the first line of setName runs the obj reference points to the person object that was created outside the function – this object has its name set to Nicholas. So at this stage obj is a copy of the reference person that was created outside the function.

    When the second line of setName runs, the local obj reference is reassigned to point to a new object, so when obj.name is called on the third line it is this new object which has its name changed to Greg. The person object is unaffected by this change of name.

    In the factory function, it is not correct to say that the ‘local object is destroyed as soon as the function finishes ‘ – the local reference to the object is destroyed, but the object itself was created on the heap and still exists. If the factory function did not return o on the last line, then this object would be garbage collected because no references to it would exist once the function returned, however o is not garbage collected because a reference to it is returned by the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please have a look at the code below (excerpt from a C# book): public
These tools basically input a grammar and output code which processes a series of
These are for two separate codecademy exercises. They both pass me but I'm not
This example is from a book. It tells me that I can clone the
My specific problem is actually not about the general translation of an OO interface
These are the memory leaks that I am getting: http://twitter.com/AWEHOFFICIAL/status/221182174811004929/photo/1/large
These days I design some algorithms in python, but find first two greatest value
These days, we encountered a strange problem, some of our solr apps on tomcat
These are my many checkboxes and I need to grab the selected ones on
These days when I create a new repository on GitHub on the setup page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.