This question already has answers here:
Should I be using object literals or constructor functions?
(12 answers)
Closed 7 years ago.
I am learning JavaScript from the basics (although I program in other languages such as C#). It popped up to me the question of which is of this two ways is more efficient and should be use as general rule.
I am sure and expecting no definitive answer but I would like to know the general pros and cons.
Thank you!!
Object literals are usually the way to go. They only need to be parsed when loading the script, which can introduce various optimizations by the scripting engine.
Constructors need to be executed. That means they will be slower, but you can easily add some validation code etc. to them, and they allow the construction of complex objects with public, privileged methods and private "attributes" hidden in the constructors scope. Also, they of course construct objects that share a prototype, which you might find useful.
Not aware of any performance efficiency of one over the other. However, the literal notation seems to get its preference due to the simplicity argument, and because it avoids using constructors and new keyword.
Constructors and the new keyword are seen by some as negative features of the JavaScript language (see Crockford - JavaScript:The Good Parts). JSLint even calls out when finding new Array() or new Object() use.
Related
This question already has answers here:
What are the differences between a pointer variable and a reference variable?
(44 answers)
When to use references vs. pointers
(17 answers)
What is the difference between a pointer and a reference variable in Java?
(9 answers)
Closed 6 months ago.
I've learned programming in Java at university. The book I read, explained the concept of pointers being used for object variables. Later I checked the actual concepts of pointers & references in C++.
I know these aren't 100% applicable to other high level / scripting languages, but there are recurring patterns that are similar.
When I started learning new languages, I noticed that most of them rely on the concept of storing some sort of object identifier inside a variable. When using an accessing-operator on the object-variable, it will read it, identify the object based on the identifier and access its content. I'd say this comes pretty close to the basic idea of a pointer.
I've confirmed that behavior for PHP and JS.
Example for PHP:
https://www.php.net/manual/en/language.oop5.references.php
When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
However, I often see people mixing up the terms reference and pointer. This started confusing me a little.
Personally, I think pointers are a more elegant way to store objects.
I'd like to know if pointers are actually a common case as object-variable or if references are just as common. Do pointers have notable advantages over references?
It's hard to tell which general concept is being used, because from a programmers perspective, the internals are often abstracted away. But I'd like to have a rough understanding of what's happening under the hood. Not in all detail, but conceptually.
At the CPU level, there are pointers (and nothing else except the registers). Therefore, all so called low-level languages must have some pointer mechanism because that's how the internals work. Every variable, function, etc is a pointer at the assembly level.
At the language level however, it's a different story if and how pointers and references are used. If they are implemented in similar way, that's up to the implementation. At the language level, they are different. So you need to learn both. It's not only the concept of a pointer or a reference also, it's the whole process of memory management, variable visibility and lifetime etc.
Suggestion: Start cleanly with a good C++ book and don't take assumptions on anything. Java's memory management is quite different from C++ and C++ 11 and later memory management is also different than earlier versions.
I've one deep confusion in javascript regarding constructors. Here you go -
objects in javascript are created from the constructors, right?
and constructors are created from the constructor function, right?
and functions in javascript are also objects but functions in javascript are also created from the Function constructor, right?
So to be purely specific is it okay to say constructors in javascript are objects or is it wrong to say like that?
So, in the end, what will you call these, to be pure specific, just constructors or objects?
Math
Number
Array
Object
I've seen on MDN documentation these are listed under pre-built objects. Does that conclude constructors are objects?
Also, it is mentioned everywhere that in javascript everything is an object.
So could anyone please clarify for me if is it okay to say constructors are also objects in javascript?
If it is yes then, HOW DOES A CONSTRUCTOR BECOMES OBJECT (and vice versa) IN JAVASCRIPT, PLEASE THROW SOME LIGHT?
This question already has answers here:
Is JavaScript's "new" keyword considered harmful?
(13 answers)
Why would using new and constructor functions be bad? [closed]
Closed 9 years ago.
I have seen some articles and some claims on answers in SO questions that using new and constructor functions is wrong and should not be used.
They go against the prototype nature of JavaScript.
Could someone please enlighten me and show a situation where using new and constructor functions is so bad it should never be used?
using new and constructor functions is wrong and should not be used.
Read Is JavaScript's "new" keyword considered harmful? - No, it is not. A few (correct) arguments are
It's confusing to newbies because of hiding the prototypical concept. To quote #Aadit:
[With new] the constructor function becomes simpler. However it becomes very
difficult to explain prototypal inheritance to a person who knows
nothing about it. It becomes even more difficult to explain it to a
person who knows classical inheritance.
Constructors do silently fail when forgetting new
In a few instances, the pure Object.create approach is cleaner
Building a class hierarchy is complicated and often done wrong
However, once you understand these, new is harmless. Actually, every time you need instance initialisation plus prototypical inheritance, constructors with new are the way to go.
They go against the prototype nature of JavaScript.
This will never change. I hardly can imagine why anyone would criticise this, prototypical inheritance is far more powerful than class inheritance. Probably they are only arguing against the syntax.
Could someone please enlighten me and show a situation where using new and constructor functions is so bad it should never be used?
It should not be used when not needed. Singletons can easily be created using the module pattern and object literals; inheritance does not help here. See Is it right to think of a Javascript Function Expression that uses the 'new' keyword as 'static' for an example.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
prototype based vs. class based inheritance
This question came up at work the other day - what's the difference between a class-based language like Python, and a prototype-based language like Javascript? Aside from differing approches, both ways seem very similar and we struggled to find something that a class-based language could do that a prototype-based language couldn't, or vice-versa.
Can anybody elaborate or go into any detail on how they differ fundamentally?
I haven't found much online about the differences, just sites that show you how to accomplish one with the other (such as this: Simulating classes with prototypes in JavaScript)
Any enlightenment appreciated!
Check out this article. It is a detailed article discussing the differences between class-based and prototype-based languages.
Copy of the table summarizing the differences:
Class-based (Java)
Class and instance are distinct entities.
Define a class with a class definition; instantiate a class with constructor methods.
Create a single object with the new operator.
Construct an object hierarchy by using class definitions to define subclasses of existing classes.
Inherit properties by following the class chain.
Class definition specifies all properties of all instances of a class. Cannot add properties dynamically at run time.
Prototype-based (JavaScript)
All objects are instances.
Define and create a set of objects with constructor functions.
Same.
Construct an object hierarchy by assigning an object as the prototype associated with a constructor function.
Inherit properties by following the prototype chain.
Constructor function or prototype specifies an initial set of properties. Can add or remove properties dynamically to individual objects or to the entire set of objects.
It seems like you're familiar with the actual languages, so you know what the difference is, right? I guess you're asking about the differences at a deeper, maybe more "philosophical", level.
Class-based languages tend to work from the top down, general to particular. The classic example would be where you define a 'Vehicle' class, and then subclasses like 'Car', 'Train'.
A prototype-based language would instead tend to start with the particular, in fact start with an instance of the particular and modify that.
I like this: http://steve-yegge.blogspot.ie/2008/10/universal-design-pattern.html
In the end it's not a question of if you can do inheritance in JS or whether there is something that you can do in one language but not the other. It's a deep difference in their ways of approaching problem solving. For a particular problem a good idiomatic solution that made best use of the language's features would probably be quite different in a prototype-based language from one in a class-based language.
The JavaScript guide of MDN has some good points, take a look: https://developer.mozilla.org/en/JavaScript/Guide/Details_of_the_Object_Model
Well, they're both (usually) Turning Complete. :)
Classes are about enforcing encapsulation, at the expense of some (or a lot of) run-time extensibility. Prototypes encourage encapsulation and allow almost unlimited run-time extension.
Seriously, prototype-based OO makes it very easy -- some would say dangerously easy -- to dynamically extend/derive from an object. Prototype languages therefore do not coexist easily with static typing. So if you like how compilers catch type errors, you will almost certainly be excluding prototype-based languages.
There's actually a very good discussion here: http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/ although there are others which are just as good I liked that one.
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 6 years ago.
Improve this question
Reading through the following question, I feel the majority of the answers miss the point of why some people (Crockford) choose not to use the "new" keyword. It's not to prevent accidental calling of a function without the "new" keyword.
According to the following article by Crockford regarding prototypal inheritance, he implements an object creation technique that more clearly demonstrates the prototypal nature of JS. This technique is now even implemented in JS 1.8.5.
His argument against using new can be more clearly summed up as:
"This indirection was intended to make the language seem more familiar to classically trained programmers, but failed to do that, as we can see from the very low opinion Java programmers have of JavaScript. JavaScript's constructor pattern did not appeal to the classical crowd. It also obscured JavaScript's true prototypal nature. As a result, there are very few programmers who know how to use the language effectively."
I don't necessarily consider "new" harmful, but I do agree that it does "obscure JavaScript's true prototypal nature," and therefore I do have to agree with Crockford on this point.
What is your opinion of using a "clearer" prototypal object creation technique, over using the "new" keyword?
You right, using new is considered harmful as it doesn't place enough emphasis on OO in JavaScript being prototypical. The main issue is that acts too much like classical classes and one should not think about class. One should think about Object's and creating new Objects with existing objects as blueprints.
new is a remnant of the days where JavaScript accepted a Java like syntax for gaining "popularity".
These days we do Prototypical OO with Object.create and use the ES5 shim.
There is no need for new anymore.
Before the days of ES5 being commonly implemented (only FF3.6 and IE8 are slacking) we used new because we had little choice. someFunction.prototype was the way to do prototypical inheritance.
I recently wrote up a "nice" demonstration of Object.create although I still need to iron out some kinks in the usage. The important thing is to separate Objects from factory functions.
I don't find that new obscures the prototypical nature of the language for me. Granted I've now spent some years getting to know the language well (having made the mistake originally of diving in and starting writing code without a clue how the language actually worked, to my initial cost).
I find new expressive in a way that prefixing "new" on function names or using a helper function, etc., isn't:
var fido = new Dog(); // Simple, clear
var rover = Dog(); // Very unclear (fortunately people mostly don't do this, but I've seen it)
var scruffles = newDog(); // Clearer, but hacky
var fifi = Object.create(Dog); // (Crockford) Verbose, awkward, not esp. clear
Whether it's prototypical or class-based (and most of that doesn't relate to the new keyword at all), I still think best in terms of functionality grouped together into building blocks (objects or classes or whatever) and then specific objects I can muck about without affecting those building blocks. For me, new as a means of clearly identifying my intent is neither classy or prototypical, it's just clear.
For me, new is just as vital a part of JavaScript as prototypes themselves are, as the wonderfully dexterous functions are. The is is no conflict.
And speaking practically, there are a lot more programmers out there used to using new to create new instances of things than not, and it's a widespread practice in the JavaScript community despite Crockford's efforts (don't misunderstand, I have a lot of respect for Crockford). If I'm staffing up a project, I don't want the retraining hassles.
This is not to say that the way that you define hierarchies of prototypical objects in JavaScript is a thing of beauty and a joy forever. It's a royal pain, especially without the ECMAScript5 enhancements. But define a helper function to help you wire up your hierarchies (whatever you want to call them), and you're done. (See update below.)
[And yes, the whole thing about constructor functions being called without new really is a red herring (you did say you didn't think it was a main point either). You almost never see it happen in the real world. Actually, some of what JavaScript does with that is quite nice (the way String and Number cast, for instance); other parts (what Date does **shudder**) are not so nice...]
As of ES2015 (ES6), creating hierarchies of prototypes and constructors is a breeze, thanks to the new class syntax. No need for helpers anymore.
class Base {
constructor(name) {
this.name = name;
}
greeting() {
return "Hi, I'm " + this.name;
}
}
class Derived extends Base {
constructor(name, age) {
super(name);
this.age = age;
}
greeting() {
return super.greeting() + " and I'm " + this.age + " years old";
}
}
That doesn't mean we want them for everything, of course. I use Object.create to great effect when I need to extend a single object.
My biggest problem with new is that it violates the Open/Closed principle. Because of the way that new manipulates the value of this, constructors are closed for extension, which means if you need to extend it, you must modify it, possibly breaking callers in the process.
Some examples of how factories can be extended that new doesn't allow:
Hide the details of object creation. (The rest of these are examples of why you might want to do that)
Store mutable prototypes and init functions on the factory object, accessible with this. (With new, this always refers to the newly created object).
Extend the pool of possible object types by allowing you to add capabilities to the factory object (extensible polymorphism).
Redefine the meaning of this during instantiation with .call() or .apply() (extending code-reuse).
Return a proxy to a new object in another memory space (iframe, window, different machine).
Conditionally return a reference to an existing object (Actually, you can do that with new and constructors, but then this is meaningless and misleading inside the constructor because a new instance gets allocated and then thrown away if you return something other than a new object).
Enable changes to instantiation strategy without breaking callers.
Note: I'm constantly irritated that I can't easily use any of these strategies with Backbone.js because it forces you to use new, unless you wrap it, but then you close off its standard inheritance mechanism. By way of contrast, I have never found myself irritated that jQuery uses a factory function to instantiate jQuery-wrapped DOM collections.