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() { } } }
你不能
JavaScript中没有向上关系。
举个例子:
var foo = { bar: [1,2,3] } var baz = {}; baz.bar = foo.bar;
现在,单个数组对象具有两个“父级”。
您可以做的是:
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();