access parent object in javascript
You can't.
There is no upwards relationship in JavaScript.
Take for example:
var foo = {
bar: [1,2,3]
}
var baz = {};
baz.bar = foo.bar;
The single array object now has two "parents".
What you could do is something like:
var User = function User(name) {
this.name = name;
};
User.prototype = {};
User.prototype.ShowGreetings = function () {
alert(this.name);
};
var user = new User('For Example');
user.ShowGreetings();
Access parent's parent from javascript object
JavaScript does not offer this functionality natively. And I doubt you could even create this type of functionality. For example:
var Bobby = {name: "Bobby"};
var Dad = {name: "Dad", children: [ Bobby ]};
var Mom = {name: "Mom", children: [ Bobby ]};
Who does Bobby belong to?
How can I access a parent object's method in javascript?
There are a number of ways to go about what you want, and you have to ask yourself: conceptually, how is this supposed to work?
You have to rethink the entire situation. Bottom line is, why does the engine care about what vehicle it is in? The vehicle only cares about what the engine is doing, not the other way around.
So, say you have a start
method on the vehicle. It may look like
start() {
this.engine.start().then(function(infofromengine) {
console.log('engine started', infofromengine);
}, function(errorfromengine) {
console.log('error in engine start', errorfromengine);
}
}
and in your engine you'd have that start
method that returns a Promise and whatever info the vehicle might need to know about the engine;
you might also, upon initializing the engine in the vehicle, set up all sorts of event emitters, and in your vehicle constuctor hook into the emitters.
Taking this one step further, you might have some child components that are supposed to, perhaps, be aware of eachother. So you could "wire them up" in construction. An example contrived from this might be in your vehicle constructor
constructor() {
this.checkEngineLight = new Light();
this.engine = new Engine({checkEngineLight});
}
and in engine,
constructor(config) {
let {checkEngineLight} = config;
this.engineLight = checkEngineLight;
}
someEngineAction() {
if (some_error_happens) this.engineLight.toggle(true, someerrorid);
}
there is a reason we are called "software engineers" or "software architects". we don't just write code, we have to consider how the code interacts with all the moving parts.
But to answer your question more directly, in the case where the "engine" absolutely has to have direct access to the "vehicle" you would have to tell the engine about the vehicle... just like how you told it about the check engine light: new Engine(this)
and the engine constructor would be constructor(vehicle)
JavaScript access parent object attribute
You can use call
to set the value of this
:
parent.child.displayA.call(parent); // 5
You may also be interested in binding it:
parent.child.displayA = function(){
console.log(this.a);
}.bind(parent);
parent.child.displayA(); // 5
Or you you can just use parent
instead of this
:
parent.child.displayA = function(){
console.log(parent.a);
};
parent.child.displayA(); // 5
Javascript objects: get parent
No. There is no way of knowing which object it came from.
s
and obj.subObj
both simply have references to the same object.
You could also do:
var obj = { subObj: {foo: 'hello world'} };
var obj2 = {};
obj2.subObj = obj.subObj;
var s = obj.subObj;
You now have three references, obj.subObj
, obj2.subObj
, and s
, to the same object. None of them is special.
Is there any way to access parent object?
export default {
data: {
a: "a",
b: "b"
},
fn: {
innerFn: () => console.log(default.data.b)
}
}
This might do the trick for you.
Better way of child object accessing parent object?
This is already the correct way to do this, in principle. You could make it somewhat fancier, providing an introduceParent() function in the child which checks, if the parent reference is already set, but that doesn't change the core of the matter.
While a child in your data model can belong to only one parent, Javascript doesn't know that. There could be several parents referencing the same child, for example (hearsay is that sometimes happens in nature). Indeed, the child is not an "inner class" of the parent. The child and parent are merely associated, i.e. "they somehow know each other", which is an unspecified relation, but neither is part (or property) of the other.
Related Topics
Set Default Value of JavaScript Object Attributes
How Is a JavaScript String Not an Object
Sorting Arrays Numerically by Object Property Value
How to Convert a String to Bytearray
How to Get Objects Value If Its Name Contains Dots
How to Serialize Dom Node to JSON Even If There Are Circular References
Access Elements of Parent Window from Iframe
Pass-By-Reference JavaScript Objects
How to Call an Async Function Inside a Useeffect() in React
How to Use Protractor on Non Angularjs Website
Alternative to Jquery's .Toggle() Method That Supports Eventdata
Wait for Multiple Promises to Finish
Concept - Distilling How a Promise Works
Leaderboard Ranking with Firebase
Resolve Circular References from JSON Object
Difference Between Date(Datestring) and New Date(Datestring)