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 8928313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:24:36+00:00 2026-06-15T08:24:36+00:00

I am trying to learn more advanced approaches to inheritance in Javascript, and can’t

  • 0

I am trying to learn more advanced approaches to inheritance in Javascript, and can’t figure out why my inherited object is loosing it’s “this” keyword binding in the example code below from Eloquent Javascript.

I’ve tried calling the take() function for example by using:

lantern.take(); // alerts you can not lift
Item.take.call(lantern, "the brass lantern"); // alerts you can not lift
lantern.take.call(this, "the brass lantern"); // alerts you can not lift

Neither of these binds this.name to lantern though? What am I missing/not understanding in my approach to calling the methods defined in my objects prototype? Thank you.

function forEachIn(object, action) {
  for (var property in object) {
    if (object.hasOwnProperty(property))
      action(property, object[property]);
  }
}

function clone(object) {
  function OneShotConstructor(){}
  OneShotConstructor.prototype = object;
  return new OneShotConstructor();
}

Object.prototype.create = function() {
  var object = clone(this);
  if (typeof object.construct == "function")
    object.construct.apply(object, arguments);
  return object;
};

Object.prototype.extend = function(properties) {
  var result = clone(this);
  forEachIn(properties, function(name, value) {
    result[name] = value;
  });
  return result;
};

var Item = {
  construct: function(name) {
    this.name = name;
  },
  inspect: function() {
    alert("it is ", this.name, ".");
  },
  kick: function() {
    alert("klunk!");
  },
  take: function() {
    alert("you can not lift ", this.name, ".");
  }
};

var lantern = Item.create("the brass lantern");
lantern.kick(); // alerts klunk
  • 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-15T08:24:38+00:00Added an answer on June 15, 2026 at 8:24 am

    Unlike console.log, alert takes only one argument. You’d need to concat the strings:

    alert("you can not lift " + this.name + ".");
    

    With that, both lantern.take(); and Item.take.call(lantern); – which are equivalent – will alert “you can not lift the brass lantern.”, while your third example depends on the value of the this keyword in the current execution. Btw, your take function does not take an argument.

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

Sidebar

Related Questions

I'm trying to learn more about the javascript libraries available and what each might
So I'm trying to learn more about lambda expressions. I read this question on
I am trying to learn more about java. This program is an attempt to
I'm trying to learn how to query Mongo in more advanced ways. Let's say
I'm trying learn more about RavenDB and at the moment to focus on how
I am trying to learn more about the DOM and have been writing some
I'm trying to learn more about the Dispose method and how things happening in
I'm trying to learn more about WPF. I ran through an online tutorial that
I'm trying to learn more about WPF and I've read a bit about Model-View-ViewModel
I am trying to learn more about the PHP function sprintf() but php.net did

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.