Self-references in object literals / initializers
Well, the only thing that I can tell you about are getter:
var foo = { a: 5, b: 6, get c() { return this.a + this.b; }}
console.log(foo.c) // 11
How can a JavaScript object refer to values in itself?
Maybe you can think about removing the attribute to a function. I mean something like this:
var obj = { key1: "it ", key2: function() { return this.key1 + " works!"; }};
alert(obj.key2());
Self reference in object literals
You can use a for... in
statement to iterate through the properties of the object and calculate the average individually by summing the scores for game1 and game2 and dividing by 2.
var scores = {
week1: {
game1: 55,
game2: 62,
},
week2: {
game1: 71,
game2: 67
}
};
for (const x in scores) scores[x].average = (scores[x].game1 + scores[x].game2) / 2;
console.log(scores);
Javascript literal object, reference to itself
Following up on Qeuntin's response you would use the following to achieve what you're looking for
var foo = {
self: false,
init: function(){
self = this
self.doStuff();
},
doStuff: function(){
alert('doing stuff');
},
}
EDIT: Since it's been pointed out that whilst this solves OP's problem (i.e it works) it isn't exactly how you should go about it. So, here's a scoping reference.
function A()
{
//Semi-private / hidden var
var pVar = "I'm a private, err hidden, variable",
//fn (technically a var)
pFn = function(){},
//empty var, placholder for hidden fn
privatePlaceholderFn;
//Instance-time... public fn
this.instancePublicFn = function()
{
console.log("--- instace public ---");
//Print hidden var to cosole
console.log(pVar);
//Call hidden fn
instancePrivateFn();
console.log("--->Setting private from instance public")
//Set the hidden fn
setPrivate();
console.log("--- / instance public ---");
}
//Pass fn to private method.
this.setPrivFromOutside = function(fn)
{
setPrivateFromPrivateYetOutside(fn);
}
//Set the hidden fn
this.iPFnPlaceholderSetter = function(fn)
{
privatePlaceholderFn = fn;
}
//Call the semi-private / hidden fn
this.callPrivate = function()
{
privatePlaceholderFn();
}
//A misnomer, proves the scope exists. See "function setPrivate()"
this.setPrivateFromInstance = function()
{
//Prove scope exists
console.log(privatePlaceholderFn);
console.log("Private From instance - gets inside scope");
}
//Set hidden fn from private method
function setPrivate()
{
privatePlaceholderFn = function()
{
//Show scope exists
console.log(pVar);
}
}
//Set the hidden fn from hidden method
function setPrivateFromPrivateYetOutside(fn)
{
//fn's scope won't resolve to inside
privatePlaceholderFn = fn;
}
//Private / hidden messager
function instancePrivateFn()
{
//Just loggin' something
console.log("Instance Private method");
}
}
//Add an object method to the prototype
A.prototype.protoPuFn = function(){
console.log("---> Private var from object literal method");
//console.log(pVar)
}
//...
a = new A();
//Add object literal fn
a.objFn = function()
{
console.log("Object literal defined public fn - Gets outside scope");
//console.log(pVar);
}
//Set private / hidden placeholder fn
a.iPFnPlaceholderSetter(function()
{
console.log("Hidden fn, passed through instance public - gets outside scope");
//console.log(pVar);
});
//Attempt to read hidden var
console.log(a.pVar);
//Call object literal defined fn
a.objFn();
//Call the hidden fn
a.callPrivate();
//Call prototype added fn
a.protoPuFn();
//Call instance added public fn
a.instancePublicFn();
//Call private / hidden method (set
a.callPrivate();
//Same as iPFnPlaceholderSetter except the param is passed to a hidden method, before seting.
a.setPrivFromOutside(function()
{
console.log("-->Passed from outside, through public then private setters");
//console.log(pVar)
})
//Call the hidden method
a.callPrivate();
//Set hidden fn from instance public
a.setPrivateFromInstance();
//Call the hidden method.
a.callPrivate();
//Use evi(a)l fn to steal scope.
a.evil("this.meth = function(){console.log(pVar)}");
//Call fn with stolen scope
a.meth();
Object referencing its own property on initialization
Alternatively, you can use a self starting function to give you a similar affect to what you are looking for:
var a = (function() {
var b = 10;
return {
b:b,
c:b*2
}
})();
console.log(a.c);
Referencing a javascript object from within its declaration
Attempts to access variables inside functions are not resolved until the function is called … which will be after the object is created.
Related Topics
How to Fix Blurry Text in My Html5 Canvas
How to Detect Which One of the Defined Font Was Used in a Web Page
How to Display an Image Stored as Byte Array in Html/JavaScript
How to Use Sockets in JavaScript\Html
What Is Innerhtml on Input Elements
Pass Vars to JavaScript Via the Src Attribute
Ternary Operator in Angularjs Templates
Html "Overlay" Which Allows Clicks to Fall Through to Elements Behind It
Flask - Calling Python Function on Button Onclick Event
Bootstrap Close Responsive Menu "On Click"
JavaScript Get Element by Name
Document.Write() Overwriting the Document
Html5 Canvas Camera/Viewport - How to Actually Do It
Onclick Open Window and Specific Size
The Usestate Set Method Is Not Reflecting a Change Immediately