I want a main object M containing a sub-object S which has some method E which has a private variable P. I also want the method E to have access to M via another variable V. For the private variables I'm doing this:
M.S = function () {
var P,
V; // how to set V to M?
return {
E: function () {
// stuff goes here
}
}
}();
One solution I came up with was to remove the () at the last line, and then calling the anonymous S-creating function as a method of M. this solves the problem, but I'm thinking there might be a more elegant way to go about it.
M.S = function () {
var P,
V = this;
return {
E: function () {
// stuff goes here
}
}
};
M.S = M.S()
Mostly I need to know what is good practice for this, since I'm new to private variables in Javascript.
A pretty straightforward method to do this is:
M.S = function (V) { // <-- V is declared locally
var P;
return {
E: function () {
// stuff goes here
}
};
}(M);
V is locally declared through the formal parameter. M's reference is assigned to V, through function(V){...}(M);.
Even when M is redeclared at a later point, V will still point to the right object.
What about this? You invoke S in context of M:
M.S = function () {
var P,
V = this; // how to set V to M?
return {
E: function () {
// stuff goes here
// you can refer M via V reference
}
}
}.call(M);
Related
Is it possible inside list to get all available functions - a, b, c without their enumeration and without using window?
(function(){
function a() { return 1; }
function b() { return 2; }
function c() { return 3; }
function list()
{
return [a, b, c];
}
})();
No, it's not possible with functions declared directly in the current scope.
To achieve this, you would have to assign the functions to some property of the scope, i.e.:
(function() {
let funcs = {};
funcs.a = function() {
return 1;
}
...
function list() {
return Object.values(funcs);
}
});
NB: Object.values is ES7, in ES6 use:
return Object.keys(funcs).map(k => funcs[k]);
or in ES2015 or earlier use:
return Object.keys(funcs).map(function(k) { return funcs[k] });
If you haven't even got Object.keys, give up... ;)
I understand where you are trying to get. So perhaps this is the closest thing to what you requested, without using the window name (the same object though):
// define a non-anonymous function in the global scope
// this function contains all the functions you need to enumerate
function non_anon() {
function a() { return 1; }
function b() { return 2; }
function c() { return 3; }
function list() { return [a, b, c]; }
// you need to return your `list` function
// i.e. the one that aggregates all the functions in this scope
return list;
}
// since in here the purpose is to access the global object,
// instead of using the `window` name, you may use `this`
for (var gobj in this) {
// from the global scope print only the objects that matter to you
switch (gobj) {
case 'non_anon':
console.info(gobj, typeof this.gobj);
console.log(
// since you need to execute the function you just found
// together with the function returned by its scope (in order to `list`)
// concatenate its name to a double pair of `()` and...
eval(gobj + '()()') // evil wins
);
break;
}
}
Is it possible to have a self executing function which is an objects property value assign values to other properties in the object?
e.g. - what I would like to do is this:
var b={
c:'hi',
d:null,
e:new function(){this.d=5}
};
But the "this" inside the new function seems to refer to b.e. Is it possible to access the b.e parent (i.e. b) from inside the function?
This is how you do it.
Often called the module pattern (more info)
var b = function () {
var c = 'hi';
var d = null;
return {
c : c,
d : d,
e : function () {
// this function can access the var d in the closure.
d = 5;
}
}
}();
You can access the value within the function, you just need to get rid of the new, i.e.
e: function () {
this.d = 5;
}
I am creating an attribute in a javascript object by replacing some strings in an existing object, as a side effect I want to make some additional changes to a third property, which I try to access with this.property however in the replace function this is referring to the window instead of my 'master' object. How can I pass in the encapsulating object, so that I can use this to access third property.
b = {
a: 'onetwothree',
count: 0,
rp: function () {
this.c = this.a.replace(/e/g, function (str, evalstr) {
this.count++; // <-- this is refering to window.
return 'e' + this.count
})
}
};
b.rp();
b.c = 'oneNaNtwothreNaNeNaN whereas I want it to be one1twothre2e3
rp: function () {
this.c = this.a.replace(/e/g, function (str, evalstr) {
this.count++;
return 'e' + this.count
}.bind( this )) // <-- bind the function
}
Cache the this context in another variable.
rp: function () {
var self = this; // Cache here
this.c = this.a.replace(/e/g, function(str, evalstr) {
return 'e' + (++self.count); // Use here
});
}
Protip: ++self.count gives the new value after incrementing.
You can usually solve this by making use of the closure you're creating, like this:
b = {
a: 'onetwothree',
count: 0,
rp: function () {
var self = this; // <-- Create a variable to point to this
this.c = this.a.replace(/e/g, function (str, evalstr) {
self.count++; // <-- And use it here
return 'e' + self.count; // <-- And here (also added the ;)
})
}
};
b.rp();
More to explore (disclosure: both are posts on my blog):
You must remember this
Closures are not complicated
What is the best way to deal with the following situation in JavaScript.
I have three methods (m1, m2, m3) and the last one (m3) depends from the results of the others two (m1, m2).
In this way it works, but I am curious to know if there is a better way to write the code in this situation, especially for future developers that will read the code.
var O = function () {
this.p = 0;
}
O.prototype.makesomething = function () {
var that = this;
that.m1();
that.m2();
that.m3();
}
O.prototype.m1 = function () {O.p++}; // it changes the value O.p
O.prototype.m2 = function () {O.p++}; // it changes the value O.p
O.prototype.m3 = function () {return O.p}; // m3 depends by m1, m2 because it needs to get the update value of O.p
First, I don't know for sure, but putting this.p = 0 inside O does not make sense in combination with O.p. You probably mean this.p inside m3, when referring to the instance.
Anyway, if you are looking for readability, you could make some simple but idiomatic functions like this: http://jsfiddle.net/ZvprZ/1/.
var O = function () {
this.p = 0;
}
O.prototype.makesomething = function () {
var that = this;
var result = when( that.m1(), that.m2() )
.then( that.m3() );
return result;
}
O.prototype.m1 = function () {this.p++};
O.prototype.m2 = function () {this.p++};
O.prototype.m3 = function () {return this.p};
The when/then can be rather straight-forward since it does not do anything than making it more readable:
(function(window) {
var when, then, o;
when = function() {
return o; // just return the object so that you can chain with .then,
// the functions have been executed already before executing
// .when
};
then = function(a) {
return a; // return the result of the first function, which has been
// executed already (the result is passed)
};
o = { when: when,
then: then };
window.when = when; // expose
window.then = then;
})(window);
I am having a JavaScript namespace say
A={
CA: function() {
this.B();
},
B: function() {
var test='test';
var result='t1';
C: function() {
this.test='test1';
.....
.....
return 'test1';
}
result=this.C();
return result;
}
}
Now when I am executing such code it is giving that TypeError: this.C is not a function. Can somebody tell me why it is so. I know it is something related with lexical scoping but am unable to understand this.
You have to be careful when you use this to identify anything in Javascript because each time you change scope "this" changes.
Assigning the 'this' reference to it's own variable helps get around this.
var a = new function() {
var self = this;
self.method = function() { alert('hiya'); };
var b = function() {
this.method(); // this isn't 'a' anymore?
self.method(); // but 'self' is still referring to 'a'
};
};
I think the problem is that when this.C() is executed inside the function referred to by B, this refers to the object that contains B, that is, object A. (This assumes B() is called within the context of A)
The problem is, C does not exist on the object A, since it's defined within B. If you want to call a local function C() within B, just use C().
EDIT:
Also, I'm not sure what you've posted is valid JavaScript. Specifically, B should be defined this way, since you can't use the object:property syntax within a function.
B: function()
{
var test='test';
var result='t1';
var C = function()
{
this.test='test1';
return 'test1';
}
result=C();
return result;
}
I am actually surprised that your code doesn't give you error on the 'C:' line.
Anyway, your syntax to define a function is not correct. Define it using the var keyword. Also, notice that I created the 'closure' so that the function C can access 'this'. See the code below:
A={
CA: function()
{
this.B();
},
B: function()
{
var test='test';
var result='t1';
var self = this;
var C = function()
{
self.test='test1';
.....
.....
return 'test1';
}
result=C();
return result;
}
}
If you want to assign C to 'this' object, you can also do:
A={
CA: function()
{
this.B();
},
B: function()
{
var test='test';
var result='t1';
var self = this;
this.C = function()
{
self.test='test1';
.....
.....
return 'test1';
};
result= this.C();
return result;
}
}
Solution for calling methods from another method. (Essentially the pointer "this" must be assigned to a variable and the new variable used in place of this.)
function myfunction(){
var me = this;
me.method1 = function(msg){ alert(msg); }
me.method2 = function(){
me.method1("method1 called from method2");
}
}
var f as new myfunction();
f.method2();
This example shows how one can call a method from within another method or from outside using an instance of the function.