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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:09:55+00:00 2026-06-10T05:09:55+00:00

I’m trying to get used to the real prototypal inheritance of JavaScript (ECMAScript 5)

  • 0

I’m trying to get used to the “real” prototypal inheritance of JavaScript (ECMAScript 5) but somehow my mind seems to be stuck in the classical inheritance pattern.

I’d like to create a Vector object which performs simple operations such as adding, subtracting etc.

Now there are two scenarios:

  • #1: Adding Vector B “to” Vector A (Vector A gets modified)
  • #2: Adding Vector B “to” Vector B (a new Vector C is created, which is the sum of A and B)

In classical inheritance I’d create an instance method for scenario #1 and a static method for case #2 but it seems like there are no static functions in prototypal inheritance.

So, what’s a clean way to realize these two scenarios?

Here’s what I’ve got so far:

var Vector = {

  x: 0,
  y: 0,
  z: 0,

  initialize: function(x,y,z) {
    this.x = x;
    this.y = y;
    this.z = z;
    return this;
  },

  add: function(vec) {
    this.x += vec.x;
    this.y += vec.y;
    this.z += vec.z;
    return this;  
  },

  print: function() {
    console.log('x:', this.x, 'y:', this.y, 'z:', this.z);
  }
};

var firstVector = Object.create(Vector).initialize(1,2,3);
var secondVector =  Object.create(Vector).initialize(4,5,6);

firstVector.print();  // Outputs: x:1, y:2, z:3
secondVector.print(); // Outputs: x:4, y:5, z:6

firstVector.add(secondVector); 
firstVector.print(); // Outputs: x:5,y:7,z:9


// What I'm looking for:

var thirdVector = Vector.add(firstVector, secondVector);

Thanks for any advice!

Update:

Here is my attempt to implement a static function using Paul’s advice (thanks!):

var vectorPrototype = {
  hello: function() { console.log('hello I am the prototype'); }
};

var Vector = Object.create(vectorPrototype);

Vector.hello = function() { console.log('hello I am the static function'); };

Vector.init = function() {
  return Object.create(vectorPrototype);
}

var vec1 = Vector.init();

vec1.hello(); // says: 'hello I am the prototype'
Vector.hello(); // says: 'hello I am the static function'
  • 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-10T05:09:57+00:00Added an answer on June 10, 2026 at 5:09 am

    Your Vector object is really just the prototype. You can use it with the Object.create function to create your Vector base/sub class. Then stick your static properties on your newly created Vector class. See here: http://jsfiddle.net/agYNc/1/

    var vectorPrototype = {
    
      x: 0,
      y: 0,
      z: 0,
    
      initialize: function(x,y,z) {
        this.x = x;
        this.y = y;
        this.z = z;
        return this;
      },
    
      add: function(vec) {
        this.x += vec.x;
        this.y += vec.y;
        this.z += vec.z;
        return this;  
      },
    
      print: function() {
        console.log('x:', this.x, 'y:', this.y, 'z:', this.z);
      }
    };
    
    //create your base Vector type
    var Vector = Object.create( vectorPrototype );
    
    
    //Your static functions here
    Vector.staticFunction = function ( vec1, vec2 ) {};
    
    
    var firstVector = Object.create(Vector).initialize(1,2,3);
    var secondVector =  Object.create(Vector).initialize(4,5,6);
    
    firstVector.print();  // Outputs: x:1, y:2, z:3
    secondVector.print(); // Outputs: x:4, y:5, z:6
    
    firstVector.add(secondVector); 
    firstVector.print(); // Outputs: x:5,y:7,z:9​
    

    Here is a good example of using Object.create with inheritance, static and instance properties. https://github.com/webadvanced/takeCommand/blob/master/src/takeCommand.module.js

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

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I am trying to render a haml file in a javascript response like so:
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.