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

  • Home
  • SEARCH
  • 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 6697915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:30:58+00:00 2026-05-26T06:30:58+00:00

I am learning more advanced OO tactics for javascript coming from a C# background

  • 0

I am learning more advanced OO tactics for javascript coming from a C# background and am wondering about how to or if its even a good idea to implement prototype based validation. For instance when an object or function requires one of its parameters to satisfy a certain interface, you could check its interface like so,

var Interface = function Interface(i) {
   var satisfied = function (t, i) {
      for (var key in i) {
         if (typeof t !== 'object') {
            return false;
         }
         if (!(key in t && typeof t[key] == i[key])) {
            return false;
         }
      }
      return true;
  }
  this.satisfiedBy = function (t) { return satisfied(t, i); }
}

// the interface
var interfacePoint2D = new Interface({
    x: 'number',
    y: 'number'
}); 

// see if it satisfies
var satisfied = interfacePoint2D.satisfiedBy(someObject); 

I came up with this strategy to validate an object by its interface only, ignoring the internal implementation of the object.

Alternatively say you are using prototype-based inheritance, should you or should not validate parameters based on their prototype functions? I understand that you’d use a prototype to implement default functionality whereas an interface doesn’t specify any default functionality. Sometimes the object you are passing into a function might need certain default functionality in order for that function to work. Is it better to only validate against an interface, or should you ever validate against a prototype, and if so, whats the best way to do it?

EDIT — I am providing some more context as to why I am asking this,

Say for instance in online game design (games written mostly in javascript). There are 2 main reasons I am interested in validation within this context,

1) Providing a strong public API for modding the game if desired

2) Preventing (or atleast discouraging greatly) potential cheaters

Which requires a balance between customizability and abuse. Specifically one situation would be in designing physics engine where objects in the game react to gravity. In a realistic system, users shouldn’t be able to add objects to the system that do not react to gravity. The system has a function that expresses the global effect of gravity at any given point:

function getGravityAt(x, y) {
      // return acceleration due to gravity at this point
}

And objects which react have a method that uses this to update their acceleration:

function update() {
      this.acceleration = getGravity(this.position); 
}

The minimum thing to do might be to ensure that any object added to the system has an ‘update’ method, but you still aren’t ensuring that the update() method really is intended to react to gravity. If only objects that inherit from a prototypical update() method are allowed, then you know at least to some degree everything in the system reacts realistically.

  • 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-26T06:30:59+00:00Added an answer on May 26, 2026 at 6:30 am

    This is a pretty subjective question. I’ll pass on the question of whether it’s a good idea to do interface-based validation in Javascript at all (there may well be good use-cases for it, but it’s not a standard approach in the language). But I will say that it’s probably not a good idea to validate objects based on their prototypes.

    If you’re validating by interface at all, you’re probably working with objects created by other programmers. There are lots of ways to create objects – some rely on prototypes, some do not, and while they each have their proponents, they’re all valid and likely approaches. For example:

    var Point = function(x,y) {
        return {
            x: function() { return x },
            y: function() { return y }
        };
    };
    
    var p = new Point(1,1);
    

    The object p conforms to an interface similar to yours above, except that x and y are functions. But there’s no way to validate that p satisfies this interface by inspecting its constructor (which is Object()) or Point.prototype. All you can do is test that p has attributes called x and y and that they are of type "function" – what you’re doing above.

    You could potentially insist that p has a specific ancestor in its prototype chain, e.g. AbstractPoint, which would include the x and y functions – you can use instanceof to check this. But you can’t be sure that x and y haven’t been redefined in p:

    var AbstractPoint = function() {};
    AbstractPoint.prototype.x = function() {};
    AbstractPoint.prototype.y = function() {};
    
    var Point = function(x,y) {
        var p = new AbstractPoint(x,y);
        p.x = "foo";
        return p;
    }
    
    var p = new Point(1,1);
    p instanceof AbstractPoint; // true
    p.x; // "foo"
    

    And perhaps more importantly, this makes it harder to drop in custom objects that also satisfy the interface but don’t inherit from your classes.

    So I think what you’re currently doing is probably the best you can hope for. In my experience, Javascript programmers are much more likely to use on-the-fly duck-typing than to try to mimic the capabilities of statically typed languages:

    function doSomethingWithUntrustedPoint(point) {
        if (!(point.x && point.y)) return false; // evasive action!
        // etc.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As I am learning more about rails, and breaking my design thinking from ASP.Net
Can anyone suggest some good materials for learning more about the Linux filesystem and
Ok I've been learning some of the more advanced aspects of Javascript and now
Does anyone have some good resources on learning more advanced regular expressions I keep
I have been learning more and more javascript; it's a necessity at my job.
I am interested in learning more about manipulating network traffic to utilize LAN only
I'm currently learning a bit more about Linq-To-Entities - particularly at the moment about
i just got some more questions while learning PHP, does php implement any built
I am still learning javascript and the more i learn the more i ask
I am learning advanced PHP standards and trying to implement new and useful methods.

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.