Why would these three lines of code result in the definition of a Javascript "class"?

tacos_tacos_tacos

The Javascript microlibrary augment.js is sometimes used to include OOP-like features in Javascript code. Despite working with Javascript on a daily basis I am still somewhat naive about the inner workings of the language, so I often rely on or borrow from patterns from other libraries to avoid pitfalls.

One function in the augment.js library is defclass, which is quite short:

augment.defclass = function (prototype) {
        var constructor = prototype.constructor;
        constructor.prototype = prototype;
        return constructor;
    };

My question is: how is this anything like defining a class? It looks like all it does is 1) set the prototype of the input's constructor to be the input itself, and then 2) return the constructor. What does that have to do with defining a class (or something like a class)?

Andrea Casaccia

how is this anything like defining a class?

The question you need to answer first is: What is a class in Javascript?

From: Object vs Class vs Function

As you must already be aware by now there are no classes in JavaScript. Instead functions in JavaScript may be made to behave like constructors by preceding a function call with the new keyword. This is known as the constructor pattern.

From: JavaScript: The Good Parts Douglas Crockford, O'Reilly Media, 8 May 2008, page 29

If a function is invoked with the new prefix, then a new object will be created with a hidden link to the value of the function's prototype member, and this will be bound to that new object.

So again there are no classes - you have functions that can be invoked with new. If those functions have a prototype property, then the new objects created are inheriting from that prototype.

Everything you need to do to define a Javascript function to be used as a constructor is setting the properties you want instances to inherit on its prototype property and (as you pointed out in your question) augment.defclass is doing that.

The reason for having such a function has been already explained by minitech in his answer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Given the following class definition and lines of code

From Dev

Why is the result of this JavaScript code "undefined"?

From Dev

Why would one use IEnumerable<T> in an interface definition if the class implementing the interface will be using List<T>?

From Dev

Why aren't these lines of code returning any result?

From Dev

Why aren't these lines of code returning any result?

From Dev

Why would an event listener in a JavaScript class see old context variable?

From Dev

JavaScript class definition local scope enable global code completion with tern

From Dev

JavaScript class definition local scope enable global code completion with tern

From Dev

Why would glDrawArrays result in EXC_BAD_ACCESS (code=2, address=0x0)?

From Dev

Why would the following code run?

From Dev

why would curl and wget result in a 403 forbidden?

From Dev

why would curl and wget result in a 403 forbidden?

From Dev

Why is no result in this code?

From Dev

Javascript: Class definition using Websocket

From Dev

Are these lines of JavaScript code equivalent?

From Dev

How to optimize the following three lines of code

From Dev

why would you need an "initialize" in ruby class?

From Dev

why would a fragment class may not be valid?

From Dev

Why would a template class have an unused type?

From Dev

Why would you use a different class variable?

From Dev

Why would this simple java code not compile?

From Dev

Why would this swift code still be performing the segue?

From Dev

Why would this command have exit code 1

From Dev

Why would you need to emit IL code?

From Dev

why would such code cause duplicate form submitting?

From Dev

Why would a file have NO color code?

From Dev

Why is python skipping these lines of code?

From Dev

why would THREE.ColladaLoader halt all activity on the site?

From Dev

Why Object Class definition makes difference

Related Related

  1. 1

    Given the following class definition and lines of code

  2. 2

    Why is the result of this JavaScript code "undefined"?

  3. 3

    Why would one use IEnumerable<T> in an interface definition if the class implementing the interface will be using List<T>?

  4. 4

    Why aren't these lines of code returning any result?

  5. 5

    Why aren't these lines of code returning any result?

  6. 6

    Why would an event listener in a JavaScript class see old context variable?

  7. 7

    JavaScript class definition local scope enable global code completion with tern

  8. 8

    JavaScript class definition local scope enable global code completion with tern

  9. 9

    Why would glDrawArrays result in EXC_BAD_ACCESS (code=2, address=0x0)?

  10. 10

    Why would the following code run?

  11. 11

    why would curl and wget result in a 403 forbidden?

  12. 12

    why would curl and wget result in a 403 forbidden?

  13. 13

    Why is no result in this code?

  14. 14

    Javascript: Class definition using Websocket

  15. 15

    Are these lines of JavaScript code equivalent?

  16. 16

    How to optimize the following three lines of code

  17. 17

    why would you need an "initialize" in ruby class?

  18. 18

    why would a fragment class may not be valid?

  19. 19

    Why would a template class have an unused type?

  20. 20

    Why would you use a different class variable?

  21. 21

    Why would this simple java code not compile?

  22. 22

    Why would this swift code still be performing the segue?

  23. 23

    Why would this command have exit code 1

  24. 24

    Why would you need to emit IL code?

  25. 25

    why would such code cause duplicate form submitting?

  26. 26

    Why would a file have NO color code?

  27. 27

    Why is python skipping these lines of code?

  28. 28

    why would THREE.ColladaLoader halt all activity on the site?

  29. 29

    Why Object Class definition makes difference

HotTag

Archive