var user = { Name: "Some user", Methods: { ShowGreetings: function() { // at this point i want to access variable "Name", //i dont want to use user.Name // **please suggest me how??** }, GetUserName: function() { } } }
The object to which a given property or method belongs.
Parent object and child object in the lookup relationship are determined purely on the requirement. Example: The object which has the more number of records will be the parent object and the object which has fewer records is considered as the child object.
The Object class is the parent class of all the classes in java by default. In other words, it is the topmost class of java. ...
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();
var user = { Name: "Some user", Methods: { ShowGreetings: function() { alert(this.Parent.Name); // "this" is the Methods object }, GetUserName: function() { } }, Init: function() { this.Methods.Parent = this; // it allows the Methods object to know who its Parent is delete this.Init; // if you don't need the Init method anymore after the you instanced the object you can remove it return this; // it gives back the object itself to instance it } }.Init();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With