When should one choose prototypal inheritance vs functional inheritance? [duplicate] - javascript

This question already has answers here:
Use of 'prototype' vs. 'this' in JavaScript?
(15 answers)
Closed 9 years ago.
I've been reading through JavaScript: The Good Parts and I'm currently on "Chapter 5: Inheritance."
From what I understand, using functional inheritance is preferred because it allows privacy for the properties and variables of an object while still allowing them to be called using methods outside of the object. However, it does seem like there is an advantage for prototypal inheritance because you can create a prototype object fairly easily (which makes understanding what the object is, to me, a little more concrete).
When should I choose one over the other? Should I always use functional inheritance whenever possible? Or are there "better" guidelines that I can follow to make that determination?

I've seen very little code that uses functional techniques as the primary form of inheritance. The vast majority I've seen uses prototypal inheritance. It's fast and easy to implement, and seems most natural to JavaScript.
That's not to say that functional inheritance should never be used, but I think you'll find prototypal more than sufficient for most applications.
Let's not also forget that you can still use some functional techniques within your prototypal inheritance, like giving each object it's own version of a function that closes over a variable in the constructor. So it doesn't need to entirely be one or the other.
Most important is to understand the concepts of prototypes and functions/closures. This alone will give you what you need to know in order to make appropriate decisions.

Related

Any Reason To Not Use ES6 Classes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have never really worked too in depth in Javascript. So just starting to dig into it more, I have learned about the "new" syntax for JS: MDN
I understand that basically it is exactly the same as before just with a different way to construct the code.
Obviously there is always a reason to learn techniques and variations, but what would make it worth my time to learn prototyping syntax, when using the updated syntax is more familiar to learn?
*One reason I can think of is just for understanding examples from before ES6. Almost everywhere the code is written with prototyping.
The only reason to avoid the class syntax is if you want your code to run in an environment where the class keyword is not supported. And, even then, you could write with class and transpile your code to ES5 compatible code if you wanted.
So, there's really no reason to avoid using class.
but what would make it worth my time to learn prototyping syntax
Even though you may code with class, here are some good reasons to fully understand how the prototype object works:
Javascript is a prototype-based language. Even though it is now using the class keyword, it's a prototype-based language, not really a class based language. You should know and understand what that means.
Older code will be written using assignments to the prototype so you will want to be able to quickly understand how that code works.
The class syntax is just manipulating the prototype under the covers so fully understanding how the class syntax works requires fully understanding how the prototype works.
There are programming structures that are sometimes very useful that the class syntax cannot create such as mixins where you may still need to work with the prototype.
So, I would suggest learning how objects are defined with the prototype object because then you will understand what the class syntax is actually doing. It's not particularly hard. Reading a few articles about how the prototype object works and then creating an object and then deriving from it using the .prototype object syntax is all you really need to do to get a handle on it. Then, I'd suggest creating the exact same object definition and then deriving from it using both the class syntax and by assigning to the prototype and then you can really see the parallels.

JavaScript Why manipulating __proto__ is slow? [duplicate]

This question already has answers here:
Why is mutating the [[prototype]] of an object bad for performance?
(4 answers)
Closed 7 years ago.
Relating this thread : JavaScript better way to modify function prototype, I was wondering why mutating instances' __proto__ is a slow manipulation.
I know it is deprecated, I often read it on the web. But I have never found why. Why is it really deprecated, and why is it slow ?
Will setPrototypeOf() be a better solution, as for performance ?
I was wondering why mutating instances' proto is a slow manipulation.
The people who implemented the JavaScript language in your browser made a trade-off: They wanted to support this "esoteric" feature, but made the rest of the language faster by making this manipulation slower.
You should only worry about the speed of __proto__ after you write your program. For many use cases, the extra "slowness" will only result in a few miliseconds difference in the overall program, and nobody will care.

Javascript framework for emulating OOP [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
What javascript OOP frameworks do you use?
I need a library to emulate Javascript OOP programming with constructors, members, properties(public, private), inheritance
Thanks for your answer
Javascript is fully Object Oriented Language, if you didn't know it yet, it means you don't know it well enough. Though some concepts differ from e.g. JAVA, or C# (like inheritance, encapsulation & etc.) they can be successfully simulated (using prototype chaining, closures & etc.).
take a better look at language and you'll find out that no additional emulating is needed to implement OOP behavior in it.
Check out the Prototype framework,
Featuring a unique, easy-to-use
toolkit for class-driven development
and the nicest Ajax library around,
Prototype is quickly becoming the
codebase of choice for web application
developers everywhere
You may use Coffee Script, a language that 'compiles' to JavaScript.
JavaScript is not an explicit object oriented language. But you can use it to implement all the Object oriented ideas if you know it well. I would suggest looking at following w.r.t JavaScript programming:
Defining custom objects in JS. This is done in a manner very much similar to "function" definition, that is where most beginners stumble.
Difference between Function invocation and Constructor style invocation (latter is used in creating objects)
Assigning member variables to objects. This can be achieved by using "this" keyword in the function definition(will be clear if point 2 is clear). Also "prototype" can be used for the same purpose.
Having private variables in an object: This can be achieved by using "var" keyword for variables declared inside the function (Will be clear if point no 2 is clear).
This site is a very good source to start you off with these concepts:
http://javascript.crockford.com/

How to implement or use inheritance in JavaScript? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Performing Inheritance in Javascript
What are the ways to implement inheritance in JavaScript Which one is the best and why?
I can think of only one: prototyping, with which you can do polymorphism, a bit of encapsulation and inheritance. That's the only way inheritance* is supported by JavaScript, but quite successfully and to quite a deep extend. See this nice and easy read on OO in JavaScript.
An excellent read, too, is Object Oriented JavaScript by Stoyan Stefanov, I can highly recommend it to you, your JS will never look the same again.
* EDIT: this is a special type of OO: prototype based programming, and JS supports it well, but as commented by Adam, you can trick your way through if you want to do it differently (advice: don't).
Doug Crockford lists several mechanisms for inheritance in his "Javascript: The Good Parts". I'd recommend reading that for a deep understanding. The material might also be available on line.
Since JavaScript is a class-free, object-oriented language, it uses prototypal inheritance. Read Douglas Crockford's "Prototypal Inheritance in JavaScript" to learn why: http://javascript.crockford.com/prototypal.html

prototypes versus classes [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 4 years ago.
Improve this question
Steve Yegge recently posted an interesting blog post on what he calls the universal design pattern. In there he details using prototypes as a modelling tool, instead of classes. I like the way this introduces less coupling compared to inheritance. But that is something one can get with classes as well, by implementing classes in terms of other classes, instead of inheritance. Does anyone else have success stories of using prototypes, and can maybe help explain where using prototypes is advantageous compared to classes. I guess it comes down to static modelling versus dynamic modelling, but more examples would be very welcome.
One interesting bit is that it's easy to make a prototype-based language act OO but it's difficult to make an OO language act prototype-based.
Alex Arnell's inheritance.js is a short and sweet chunk of code that makes JavaScript act OO, complete with access to the parent 'Class'.
Here's one of John Resig's solutions to the same problem: http://ejohn.org/blog/simple-javascript-inheritance/.
Chapter 16 of Programming in Lua describes object orientation in Lua. Specifically, section 16.2 gives a nice example of inheritance.
It's not entirely clear what OO as prototype would look like, aside from composition versus inheritance as you mention.
A prototype language makes complex inheritance behavior easy. You can implement multiple inheritance, mixin-like behavior, or just pick and choose what you want from one object to add to another.
Wikipedia's article mentions: "Advocates of prototype-based programming often argue that class-based languages encourage a model of development that focuses first on the taxonomy and relationships between classes. In contrast, prototype-based programming is seen as encouraging the programmer to focus on the behavior of some set of examples and only later worry about classifying these objects into archetypal objects that are later used in a fashion similar to classes."
That's not to say the prototype paradigm is all pros and no cons. If OO is more restrictive, it's because it chooses to be. I can see where all that flexibility might get you into trouble if you aren't careful.
Prototypes are a form of inheritance, it's just that objects inherit attributes and behavior directly from other objects, instead of getting their attributes and behavior from their class, which inherits from other classes.
For examples, check out any object oriented code in a prototype based language like, for example, JavaScript.
For those interested, NewtonScript was (is) a dual language: you had prototypes and you had classes. You could choose whether to inherit from a class, from a prototype or from both.

Categories