Javascript: set object variable to inner object instance [duplicate] - javascript

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.

Related

ES6 class instance from string [duplicate]

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).

Mimicking Java-style inheritance in Javascript [duplicate]

This question already has answers here:
What is the reason to use the 'new' keyword at Derived.prototype = new Base
(6 answers)
Closed 7 years ago.
I'm trying to mimic Java-style class inheritance and I thought I'd gotten it. However, I'm having an issue that when I create multiple instance of the same Javascript class, all instances are sort of created the same.
Here is the situation.
I have a Javascript class called Screen:
function Screen(screenName) {
this.current = new DataResultSet(); // this is a java class
this.current.setScreenName(screenName);
}
Screen.prototype.GetScreenName= function() {
return this.current.getScreenName();
}
Screen.prototype.GetFieldValue= function(name) {
return this.current.getValue(name);
}
// some other functions that can be called that utilize the current object.
This is the inherited class:
function screenSub1() {}
screenSub1.prototype=new Screen("screenSub1");
screenSub1.prototype.GetID = function() { return GetFieldValue("ID"); }
// some other functions that can be called, specific to this class
Now, in my code, if I do
var obj = new screenSub1();
var obj2 = new screenSub1();
The underlying "current" object for both objects are the same! Is there anyway to get around this issue? Thanks!
Julia
As far as I know, you can't (at least not easily, maybe you could find some javascript library that mimics java-like inheritance). Java and Javascript are very different when it comes to inheritance. You can check here if You want to have further information on the subject : https://developer.mozilla.org/en/docs/Web/JavaScript/Inheritance_and_the_prototype_chain

How to instantiate class dynamically [duplicate]

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

use of "that" keyword in javascript [duplicate]

This question already has answers here:
What does 'var that = this;' mean in JavaScript?
(6 answers)
Closed 10 years ago.
I have few javascript that has used the keyword "that" extensively.
I see a lot of posts talking about the javascript keyword "this".
I wanted to understand the meaning of this key word in javascript context and it's visibility/scope.
Something like
that.someFunctionaName(someParameter)
What does it mean?
I understand the keyword "this" always points to the owner of the current object.
that is not a keyword in JavaScript. I suspect the code that you have is using something in the class to define an instance of itself. For example:
function myClass()
{
var that = this;
}
By doing this, you can ensure you're referencing the object, and not another element. For example, consider the following sample:
function myClass()
{
var that = this;
$('.myele').click(function() {
// 'this' refers to the element that was clicked.
// 'that' still refers to the myClass() object.
});
}

How do I access a local variable dynamically (via a String form of its name) from a closure scope? [duplicate]

This question already has answers here:
How can I access local scope dynamically in javascript?
(4 answers)
Closed 8 years ago.
In Javascript, I'm used to being able to access variables within a known namespace "dynamically" (correct me if I'm using the wrong word here) by using the [] operator. For instance (from the global namespace):
var a = 1;
window['a']; # => 1
Or from a object-type namespace:
var a = { b: 1 };
a['b']; # => 1
And I'm familiar with the basics of how this is determined:
var a = function(){ return this['c']; };
var b = { c: 1 };
a.apply(b); # => 1;
But within a function itself, how do I access local variables I've just instantiated (or redefined) using var?
Namely, I want the following function call to return 1 but without calling a:
function(){
var a = 1;
return a;
}
You can't use window['a'] because a is defined locally, and you can't use this['a'] because this changes depending on the context from which the function is called.
In a realistic setting, I'd simply refactor to avoid dynamically creating and accessing local variables because it's generally a bad idea anyway, but as an academic question, I'm curious as to whether it's possible to access a via its string name at all.
You're mixing up local variables (which are not properties of an object) with properties (which are not local variables). There is no answer to your question, or, rather, the answer is "it can't be done".

Categories

Resources