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 11 years ago.
In JavaScript what sort of inheritance do you favour? The ECMA standard is to use prototype chaining. So I just set the prototype of the object that's doing the inheriting (monkey) to invoke a new instance of the object I want to inherit from (animal):
monkey.prototype = new animal();
I am aware there are other ways to achieve inheritance. i.e inheriting only the prototype, parasitic inheritance, deep copy, shallow copy.
Can anyone enlighten me and tell me if there is one in particular I should use? One that has benefits that outweigh the other methods.
Theres a lot better article on it here than I could ever write. http://www.crockford.com/javascript/inheritance.html
In general it depends on what you want to achieve. In general though I think duck typing is what is considered best for javascript.
I posted an article that talks about the in and outs of JS inheritance. It's relevant if you're trying to emulate class based inheritance. I don't like Douglas Crockford's approach that tries to stay away from the new keyword. I learned class based inheritance with C++ and Java and find it much easier to think about http://js-bits.blogspot.com/2010/08/javascript-inheritance-done-right.html
Related
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 want to learn Node.js. I'm sure, I will need good experience in Javascript (OOP). Is there any other technology i should know about to learn it? And also how long will it take me to starting implementing node.js taking into account i have decent experience in Javascript?
Thank You
http://www.crockford.com/javascript/inheritance.html
JavaScript is a class-free, object-oriented language, and as such, it
uses prototypal inheritance instead of classical inheritance. This can
be puzzling to programmers trained in conventional object-oriented
languages like C++ and Java. JavaScript's prototypal inheritance has
more expressive power than classical inheritance, as we will see
presently.
Javascript and Java are two different beasts.
I have been writing JavaScript for 8 years now, and I have never once
found need to use an uber function. The super idea is fairly important
in the classical pattern, but it appears to be unnecessary in the
prototypal and functional patterns. I now see my early attempts to
support the classical model in JavaScript as a mistake.
Some things I think you should look into.
Testing your code thoroughly with framework like for example mocha is very important.
learn npm to publish your own modules and to search for other modules.
A database like for example mysql, mongodb or redis and how to use them in node.js. I pesonally really like redis client library for it's insane speed.
Git(hub) to put your code into a repository(SCM).
Except from all kind of JavaScript programming experiences (OOP is not required) and depending from your former programming skills it also helpful to have a basic knowledge of:
the Posix API (what are file descriptors? what are pipes?)
what is HTTP? How does HTTP work?
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 read somewhere that using prototype to extend native objects (String, Number, Array, etc.) was a bad idea. Is this true? Why or why not?
I don't think it's bad. If you have a look at Ruby on Rails, very many native objects are extended with custom functionality and it's a very good framework.
What would be bad is if you change existing methods on native objects. this could cause unforseen consequences.
There's a great discussion about this in this video from JSConf 2011 by Andrew Dupont. http://blip.tv/jsconf/jsconf2011-andrew-dupont-everything-is-permitted-extending-built-ins-5211542
Basically the points are:
Don't extend Object.prototype
Some people might like to extend things, some people don't
You need to know what you're doing
Don't use two libraries that extend things, because it can be bad
Extending prototypes to add standard functionality is almost always ok.
I would stay clear of extending/modifying behavior of native objects.
It at least makes sense when developing in a team environment.
Simply because, months later, another developer writing another independent piece of code isn't immediately going to recognize the changed behavior unless documented somewhere and made aware of it prior to starting his task.
Instead, I suggest encapsulating/"namespace"-ing all such functionality such that somebody may chose to or not to use the modified functions.
Also, native objects and their methods are thoroughly tested for wide-ranging cases. So you you'd have to be completely sure of what you're doing before modifying native functionality.
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/
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 9 years ago.
I'm wondering if such a library exists, where the library contains only a collection of common utility functions such as trim, indexOf (for arrays), map, each, typeOf and so on...
I'm aware that jQuery provides some of the functions I listed above, but I'm looking for something that is designed purely for this (I really don't need jQuery on the server-side running node.js for instance, nor do I want to depend on it if I'm writing a non-jQuery-related JavaScript library). I've recently begun collecting an assortment of these functions for future copy/pasting, but my guess is that there are many others that I won't even know to look for, or think of using, unless someone presents them to me.
I'm fond of underscore.js; it doesn't provide string utilities such as trim; it's focused on object-oriented and functional utilities, including all of the other things you mention. Another nice thing about it is that it doesn't reference the DOM at all, so it's useful for javascript programming that isn't web-based or DOM related.
The functions you mention are all standard in ECMAScript 5. And this library implements them in such a way that you can use them in older browsers/JavaScript versions as well, in a way that will be compatible when your environment catches up with the standard:
https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js
Boiler.js is an up and coming utility library that offers many useful JavaScript utilities and is a direct competitor with Underscore.js.
jQuery provides all of those and many more, you would be better off just using it.
jQuery can sit side-by-side with other frameworks, so can be independent if another framework is present.
See: jQuery Utilities Documentation
Javascript itself has many of these functions built into the basic types. Before building your own, perhaps a copy of JavaScript: The Definitive Guide, focusing on the API reference in the back, would do you some solid good.
After that investigate frameworks, or at least being looking into how you can create your own framework for your functions (as opposed to copying and pasting). Here the module pattern would be helpful to you.
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'm a pro when it comes to HTML/CSS, and can use jQuery pretty well to move things around on my sites, but I need a good book for a NON PROGRAMMING person to learn OO JS. I just can't grasp it. I need a good book for learning! Thanks :)
If you are completely new to programming and need to learn JavaScript from the ground up, have a go at the freely available Eloquent JavaScript.
Object-oriented Programming chapter from Eloquent JavaScript is a good read. I also strongly recommend reading chapters 3-5 from JavaScript: The Good Parts to understand JavaScript Objects, Functions, Inheritance. The best I have read on the topic till now.
I highly recommend O'Reilly book JavaScript Patterns by Stoyan Stefanov.
"JavaScript: The Definite Guide" by David Flanagan is a fine one containing comprehensive information of all aspects of JS.
If you want to master JavaScript, I recommend that you Google Mr. Douglas Crockford. Both his website, and like dheerosaur suggested, his book "JavaScript: The Good Parts". The book offers insight of JavaScript including its OO aspect.