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

The Archive Base Latest Questions

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

I’m learning how to make OOP with JavaScript . Does it have the interface

  • 0

I’m learning how to make OOP with JavaScript. Does it have the interface concept (such as Java’s interface)?

So I would be able to create a listener…

  • 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-16T23:20:25+00:00Added an answer on May 16, 2026 at 11:20 pm

    There’s no notion of “this class must have these functions” (that is, no interfaces per se), because:

    1. JavaScript inheritance is based on objects, not classes. That’s not a big deal until you realize:
    2. JavaScript is an extremely dynamically typed language — you can create an object with the proper methods, which would make it conform to the interface, and then undefine all the stuff that made it conform. It’d be so easy to subvert the type system — even accidentally! — that it wouldn’t be worth it to try and make a type system in the first place.

    Instead, JavaScript uses what’s called duck typing. (If it walks like a duck, and quacks like a duck, as far as JS cares, it’s a duck.) If your object has quack(), walk(), and fly() methods, code can use it wherever it expects an object that can walk, quack, and fly, without requiring the implementation of some “Duckable” interface. The interface is exactly the set of functions that the code uses (and the return values from those functions), and with duck typing, you get that for free.

    Now, that’s not to say your code won’t fail halfway through, if you try to call some_dog.quack(); you’ll get a TypeError. Frankly, if you’re telling dogs to quack, you have slightly bigger problems; duck typing works best when you keep all your ducks in a row, so to speak, and aren’t letting dogs and ducks mingle together unless you’re treating them as generic animals. In other words, even though the interface is fluid, it’s still there; it’s often an error to pass a dog to code that expects it to quack and fly in the first place.

    But if you’re sure you’re doing the right thing, you can work around the quacking-dog problem by testing for the existence of a particular method before trying to use it. Something like

    if (typeof(someObject.quack) == "function")
    {
        // This thing can quack
    }
    

    So you can check for all the methods you can use before you use them. The syntax is kind of ugly, though. There’s a slightly prettier way:

    Object.prototype.can = function(methodName)
    {
         return ((typeof this[methodName]) == "function");
    };
    
    if (someObject.can("quack"))
    {
        someObject.quack();
    }
    

    This is standard JavaScript, so it should work in any JS interpreter worth using. It has the added benefit of reading like English.

    For modern browsers (that is, pretty much any browser other than IE 6-8), there’s even a way to keep the property from showing up in for...in:

    Object.defineProperty(Object.prototype, 'can', {
        enumerable: false,
        value: function(method) {
            return (typeof this[method] === 'function');
        }
    }
    

    The problem is that IE7 objects don’t have .defineProperty at all, and in IE8, it allegedly only works on host objects (that is, DOM elements and such). If compatibility is an issue, you can’t use .defineProperty. (I won’t even mention IE6, because it’s rather irrelevant anymore outside of China.)

    Another issue is that some coding styles like to assume that everyone writes bad code, and prohibit modifying Object.prototype in case someone wants to blindly use for...in. If you care about that, or are using (IMO broken) code that does, try a slightly different version:

    function can(obj, methodName)
    {
         return ((typeof obj[methodName]) == "function");
    }
    
    if (can(someObject, "quack"))
    {
        someObject.quack();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I would like to count the length of a string with PHP. The string
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,

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.