This question already has answers here:
Adding Prototype to JavaScript Object Literal
(5 answers)
Closed 9 years ago.
If I have an object like this:
var obj = {};
I can't extend it because it hasn't got any prototype.
Is there any way to convert this object to dynamic so that it's possible to extend it and use new keyword. Something like:
obj.prototype.property = 'value';
var newobj = new obj;
That has nothing to do with static or dynamic.
You can only use the new operator on functions, not objects.
You cannot turn an object into a function; you need to create it as a function in the first place.
the only way is the following:
var obj = function () {};
because you can only use the new keyword with constructor function. That's it!
Related
This question already has answers here:
Array() vs new Array()
(4 answers)
Closed 3 years ago.
Why are these two statements the same in JavaScript?
var a = new Array(5);
var a = Array(5);
Or, if they are not the same, what is different about them? Basically if I assign a to a different variable and mutate it, the values in the array change for each initialization in a similar way.
Why are these two statements the same in JavaScript?
var a = new Array(5);
var a = Array(5);
That's just the way Array was designed. Not all constructors have this behavior, but the Array constructor's specification is such that it can be called either with new or without.
From the ECMAScript spec:
22.1.1The Array Constructor
[...]
also creates and initializes a new Array object when called as a function rather than as a constructor. Thus the function call Array(…) is equivalent to the object creation expression new Array(…) with the same arguments.
This question already has answers here:
Create object from class name in JavasScript ECMAScript 6
(8 answers)
Closed 4 years ago.
I try to setup an instance dynamically from a string. I've read many questions about it but the answers does not work for me.
It says it's possible to use window before the name to set the instance. It does not work.
class MyClass {
// Something useful
}
let params = {};
let name = 'MyClass';
let instance = new window[name](params);
I've also tried to do this without luck (throws error):
let instance = new window['MyClass'](params);
However, this works:
let instance = new MyClass(params);
Why can't I use window in this case? Any other ideas?
Only global variables are put into window automatically.
Create an object that maps from class names to classes:
const classMap = {
"MyClass": MyClass,
"MyClass2": MyClass2,
...
};
Then use classMap[name](params) rather than window[name](params).
This question already has answers here:
Self-references in object literals / initializers
(30 answers)
Closed 6 years ago.
I'm trying to create a class to better organize my code, I decided to go the object literal route
var MyClass = {
}
I'm currently running into issues migrating some of my functionality though, previously I had global scope variables Channels for example set to an object instance. Is there a way to still do this in javascript within a class without moving the object into global scope?
var Prime = {
Channel: new __Prime__Channel(),
//The object in question
__Prime__Channel: function() {
this.Property = Value;
},
}
this throws
Undefined reference __Prime__Channel() at line 2
In global scope you could do
var Channel = new __Prime__Channel();
function __Prime__Channel() {
this.Property = Value;
}
without any errors
An object is no class, please ensure you're using the right terminology.
With an object literal it is not possible to do what you want, the only way is
var Prime = {
__Prime__Channel: function() {
this.Property = Value;
}
}
Prime.Channel = new Prime.__Prime__Channel();
However, maybe an object literal is the wrong pattern anyway. If you want to avoid global variables, look into the module pattern and IIFEs.
This question already has answers here:
What is the most efficient way to deep clone an object in JavaScript?
(67 answers)
Closed 8 years ago.
I am using a plain JavaScript object. I have to create an exact copy of the object to make changes:
var gRoll = {a:"pankaj",b:
{
a:"A",b:"c"
}}
var copy = gRoll;
copy.a = "Karma";
This is making change in the parent object. Please give me solution to create copy of the object without referring to the old one. Same like prototype design pattern in OOPS.
You're referencing the same object with copy
var gRoll = {
a:"pankaj",
b:{a:"A",b:"c"}
}
var newObject = Object.create(gRoll);
newObject.a = 'Karma';
alert(gRoll.a); //pankaj
alert(newObject.a); //Karma
what you are doing is not creating a copy but referring to the existing object with a new variable, hence the issue.
If you are using jQuery, use its extend() method which copies an exiting object... with optional parameter for deep-copying.
http://api.jquery.com/jquery.extend/
This question already has answers here:
How to execute a JavaScript function when I have its name as a string
(36 answers)
Closed 8 years ago.
I have an array with names of constructors var arr = ['Class1', 'Class2', 'Class3'].
function Class1() {
this.name = 'class1';
}
Is it possible to dynamically create instances of these classes? I mean something like
var class1Object = new arr[0]();
I tried that but it isn't working (Uncaught TypeError: string is not a function).
functions defined in "global" scope are actually created on the window object, so you can do this (as long as the code is in the head of the page, and not scoped to something specific):
function Class1(){
this.name = 'class1';
}
var className = "Class1";
var c1 = new window[className]();
Live example: http://jsfiddle.net/vdf4W/
Previously answered in this post (Google: Dynamic Instantiation In JavaScript)
Dynamic Instantiation In JavaScript