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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:16:02+00:00 2026-05-25T15:16:02+00:00

I get an error whenever I try to add the object BaseNet to IPv4Address.prototype

  • 0

I get an error whenever I try to add the object BaseNet to IPv4Address.prototype. The error:

TypeError: Cannot read property ‘ipInt’ of undefined

just doesn’t make sense. It’s behaving like the getter is actually being executed when I copy the object to the prototype. Is there a way to copy this and not get an error like this?

var _s = require('underscore')

var BaseNet = {
  get network(){ 
    return new IPv4Address((this.ipInt & this.netmask.ipInt) >>> 0);
  },
}

function IPv4Address (address){
  this.ipInt = address
  this.netmask = {}
  this.netmask.ipInt = 255
}

_s.extend(IPv4Address.prototype,BaseNet)

//also fails with generic extend:

function extend(destination, source) {
  for (var k in source) {
    if (source.hasOwnProperty(k)) {
      destination[k] = source[k];
    }
  }
  return destination;
}

extend(IPv4Address.prototype,BaseNet)

First Edit

Maybe this is an answer to my own question. Resig had a post on this and this “modififed” extend method seems to do what I’m looking for. Can someone explain? It seems that it’s a scoping problem, but i don’t understand why it was behaving like someone was actually calling the getter during the extend operation.

http://ejohn.org/blog/javascript-getters-and-setters/

function extend(a,b) {
    for ( var i in b ) {
        var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);

        if ( g || s ) {
            if ( g )
                a.__defineGetter__(i, g);
            if ( s )
                a.__defineSetter__(i, s);
         } else
             a[i] = b[i];
    }
    return a;
}

Second Edit

So I did some more experimenting and I came up with two other ways that seems to work. One uses the extend method posted earlier and another uses the defineProperties method form the ECMA spec pointed out in the comments. Is one better than the other? It seems that the “mixin” style works better for what i’m looking for, but the Object.create way also works.

var BaseNet = {}
Object.defineProperties(BaseNet, {
  "network":{
    enumerable:true,
    get: function (){return new IPv4Address((this.ipInt & this.netmask.ipInt) >>> 0)}
            }
    });

function IPv4Address (address){
  this.ipInt = address
  this.netmask = {}
  this.netmask.ipInt = 255
}
IPv4Address.prototype = Object.create(BaseNet)
var ip = new IPv4Address(12345)
ip.netmask

Alternatively, you can also do:

var BaseNet = {}
Object.defineProperties(BaseNet, {
  "network":{
    enumerable:true,
    get: function (){return new IPv4Address((this.ipInt & this.netmask.ipInt) >>> 0)}
            }
    });

function IPv4Address (address){
  this.ipInt = address
  this.netmask = {}
  this.netmask.ipInt = 255
}
extend(IPv4Address.prototype,BaseNet)
var ip = new IPv4Address(12345)
ip.netmask
  • 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-05-25T15:16:02+00:00Added an answer on May 25, 2026 at 3:16 pm

    It does not work because whilst extending, the getter is executed, at which point this.netmask is not an instance at all (there are no instances created), but in fact undefined, so accessing this.netmask.ipInt throws an error (you cannot access anything from undefined or null, it throws an error in any case).

    Have a look at the underlying _.extend code:

      _.extend = function(obj) {
        each(slice.call(arguments, 1), function(source) {
          for (var prop in source) {
            // source[prop] is fetched, so getter is executed
            if (source[prop] !== void 0) obj[prop] = source[prop];
          }
        });
        return obj;
      };
    

    You might instead want to iterate yourself and copy the getter “untouched” with a for in loop and Object.defineProperty.


    As for your edit: Those __xxx__ functions are an ugly way to get the getter/setter function without executing it. Normally, passing a function works like someFunction without parentheses. A getter, however, would automatically get executed if you access it with someGetter.

    The function you posted copies getters/setters without executing them:

    function extend(a,b) {
        for ( var i in b ) { // iterate over all properties
            // get getter and setter functions
            var g = b.__lookupGetter__(i), s = b.__lookupSetter__(i);
    
            if ( g || s ) { // if there is a getter or setter
                if ( g )
                    a.__defineGetter__(i, g); // copy getter to new object
                if ( s )
                    a.__defineSetter__(i, s); // copy setter to new object
             } else // if there is not getter nor setter
                 a[i] = b[i]; // just copy the value; nothing special
        }
        return a; // return the altered object
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I get the above error whenever I try and use ActionLink ? I've only
whenever I try to call my ejb from a client, I get this error
I get a run-time error NullPointerException whenever I try to click the options button
I get an error whenever I try to use the function gets within a
I get an error whenever I try to create a simple edge from a
I have a website that I get the below error on whenever I navigate
Whenever I build the following code, I get the error above. //Controller.h #import <UIKit/UIKit.h>
When we try to create a view within a funcion we get ERROR: there
Whenever I try to uninstall my WIX installation via Add/Remove Programs or the uninstall
Whenever I try to add a new ribbon into my Excel 2010 addin project,

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.