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

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

Related

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

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.

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/

Recommendations for an experienced programmer new to JavaScript? [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.
I come from a C/Unix background, with a lot of experience in shell scripting, and some on Perl, elisp, etc. too. But now I'm getting into some work where I'll need to be developing interactive web-based interfaces, and I need to learn JavaScript. My problem is that all the resources I've found online for learning JavaScript seem to be targeted at an audience who's never programmed, and their authors don't seem much better. As soon as I see "validating user input to take the load off your server" as one of the great uses for JS, I want to scream and I feel like I can't trust anything else the author says. ;-)
Can anyone recommend good resources for an experienced programmer wanting to learn JS as a new language? Ideally I'd like to get started online, but dead tree recommendations would be welcome too, especially if I can preview them online.
A great JavaScript book for experienced programmers is Doug Crockford's JavaScript: The Good Parts. It's short, assumes you know what you're doing, is opinionated, and is not a tutorial.
My advice: Forget what you know about object oriented programming. Attempts to apply the inheritance paradigms from an OO language have repeatedly overcomplicated many, many chunks of JS code.
Prototyping is not Class construction. Object instantiation is not Class instantiation. "Classes" are not real.
There are ways to get what you want. You can even have something akin to privates - but they are not methods or members. They are merely locally scoped. Inheritance is often faked, but with mixed results, and universally at the expense of data hiding.
Javascript is prototyped. It is not object oriented. Keep that in mind every time you think something like, "Man, an interface here would be awesome..."
https://stackoverflow.com/questions/3655530/best-javascript-book-for-an-experienced-coder/3655693#3655693
https://stackoverflow.com/questions/1594159/best-book-to-learn-web-development-for-a-professional-developer
https://stackoverflow.com/questions/74884/good-javascript-books
but I will recommend these two fantastic books, which teach me a lot.
Take a look at Eloquent JavaScript. It doesn't cover everything, but it will move you towards idiomatic JavaScript programming -- things like functional programming, closures and prototypes. (The online version comes complete with a sandbox tutorial environment.) The rest, after all, is just knowing how to use references.
Javascript Guide from Mozilla Developer Network, a simple and yet informative guide, gives beginners a big picture of JS in a short time.
https://developer.mozilla.org/en/JavaScript/Guide
JavaScript: the definitive guide is one of my favorite programming books:
http://oreilly.com/catalog/9780596101992

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.

Javascript Best Practices [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What are some good resources to learn best practices for Javascript? I'm mainly concerned about when something should be an object vs. when it should just be tracked in the DOM. Also I would like to better learn how to organize my code so it's easy to unit test.
Seconding Javascript: The Good Parts and Resig's book Secrets of the Javascript Ninja.
Here are some tips for Javascript:
Don't pollute the global namespace (put all functions into objects/closures)
Take a look at YUI, it's a huge codebase with only 2 global objects: YAHOO and YAHOO_config
Use the Module pattern for singletons (http://yuiblog.com/blog/2007/06/12/module-pattern/)
Make your JS as reusable as possible (jQuery plugins, YUI modules, basic JS objects.) Don't write tons of global functions.
Don't forget to var your variables
Use JSlint : http://www.jslint.com/
If you need to save state, it's probably best to use objects instead of the DOM.
I disagree to the "use a framework" statement to some degree. Too many people use frameworks blindly and have little or no understanding of what's going on behind the curtains.
I liked JavaScript:The Good Parts by Douglas Crockford although it's focused entirely on the language and ignores the DOM altogether.
If you don't feel like reading you can watch this video: JavaScript the good parts by Doug Crockford.
Probably the single most important thing is to use a framework, such as jQuery, or prototype, to iron out the differences between browsers, and also make things easier in general.
YUI Theatre has a bunch of videos (some with transcripts) by Steve Souders, Douglas Crockford, John Resig and others on JavaScript, YUI, website performance and other related topics.
There are also very interested google tech talks on Youtube on jQuery and other frameworks.
You can pick up a lot from Pro JavaScript Techniques, and I'm looking forward to Resig's forthcoming Secrets of the JavaScript Ninja.
As an addendum to the Crockford book, you may also want to check out this piece Code Conventions for the Javascript Programming Language. I also have a slightly different suggestion: instead of using a JS library off the bat, why not create your own? You may write a crappy library (as I did), but you'll learn something in the process. You have existing examples you can use as models. Also, to help give you an understanding of JS design patterns, I shall recommend another book, 'Pro Javascript Design Patterns'.

Categories