在javascript中访问父对象
你不能 JavaScript中没有向上关系 例如:在javascript中访问父对象,javascript,Javascript,你不能 JavaScript中没有向上关系 例如: var user = { Name: "Some user", Methods: { ShowGreetings: function() { // at this point i want to access variable "Name", //i dont want to use user.Nam
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() { }
}
}
// Make user global
window.user = {
name: "Some user",
methods: {
showGreetings: function () {
window.alert("Hello " + this.getUserName());
},
getUserName: function () {
return this.getParent().name;
}
}
};
// Add some JavaScript magic
(function () {
var makeClass = function (className) {
createClass.call(this, className);
for (key in this[className]) {
if (typeof this[className][key] === "object") {
makeClass.call(this[className], key);
}
}
}
var createClass = function (className) {
// private
var _parent = this;
var _namespace = className;
// public
this[className] = this[className] || {};
this[className].getType = function () {
var o = this,
ret = "";
while (typeof o.getParent === "function") {
ret = o.getNamespace() + (ret.length === 0 ? "" : ".") + ret;
o = o.getParent();
}
return ret;
};
this[className].getParent = function () {
return _parent;
};
this[className].getNamespace = function () {
return _namespace;
}
};
makeClass.call(window, "user");
})();
user.methods.showGreetings();
单个数组对象现在有两个“父对象”
你能做的是:
var foo = {
bar: [1,2,3]
}
var baz = {};
baz.bar = foo.bar;
大卫·多沃德就在这里。最简单的解决方案是访问
user.Name
,因为user
实际上是一个单身汉。您可以使用闭包尝试另一种方法:
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() {
// at this point i want to access variable "Name",
//i dont want to use user.Name
// **please suggest me how??**
},
GetUserName: function() { }
}
}
// Make user global
window.user = {
name: "Some user",
methods: {
showGreetings: function () {
window.alert("Hello " + this.getUserName());
},
getUserName: function () {
return this.getParent().name;
}
}
};
// Add some JavaScript magic
(function () {
var makeClass = function (className) {
createClass.call(this, className);
for (key in this[className]) {
if (typeof this[className][key] === "object") {
makeClass.call(this[className], key);
}
}
}
var createClass = function (className) {
// private
var _parent = this;
var _namespace = className;
// public
this[className] = this[className] || {};
this[className].getType = function () {
var o = this,
ret = "";
while (typeof o.getParent === "function") {
ret = o.getNamespace() + (ret.length === 0 ? "" : ".") + ret;
o = o.getParent();
}
return ret;
};
this[className].getParent = function () {
return _parent;
};
this[className].getNamespace = function () {
return _namespace;
}
};
makeClass.call(window, "user");
})();
user.methods.showGreetings();
这样怎么样
function user(name) {
var username = name;
this.showGreetings = function()
{
alert(username);
}
}
因此,您可以访问ShowGreetings中的user.Name
user.Methods.ShowGreetings.call(user, args);
老问题,但为什么你不能这样做:
var user = {
Name: "Some user",
Methods: {
ShowGreetings: function(arg) {
console.log(arg, this.Name);
},
GetUserName: function() { }
},
Init: function() {
this.Methods.ShowGreetings.call(this, 1);
}
};
user.Init(); // => 1 "Some user"
var用户={
名称:“某用户”,
方法:{
ShowGreetings:function(){
//此时我想访问变量“Name”,
//我不想使用user.Name
//**请告诉我怎么做**
var thisName=user.Name;//正如其他人所说,对于普通对象,无法从嵌套的子对象中查找父对象
但是,如果您使用递归作为帮助程序,这是可能的
我编写了一个名为的库,它允许您从子对象向上遍历到父对象
下面是一个简单的例子():
我偶然看到这篇老文章,试图记住如何解决这个问题。这是我使用的解决方案。这是从Harmes和Diaz(Apress 2008)在第8页的Pro JavaScript设计模式中派生出来的。你需要声明一个函数,然后创建一个新的函数实例,如下所示。注意Store方法可以访问“this”
作为一种变体:
function Test() {
this.x = 1;
}
Test.prototype = {
Store: function (y) { this.x = y; },
}
var t1 = new Test();
var t2 = new Test();
t1.Store(3);
t2.Store(5);
console.log(t1);
console.log(t2);
但是我不知道为什么有人会用这种奇怪的方法我知道我已经很晚了。
我写了这个简单的方法。假设你有:
var user = (obj => {
Object.keys(obj.Methods).map(option => {
const currOpt = obj.Methods[option];
if (currOpt instanceof Function) {
obj.Methods[option] = currOpt.bind(obj);
};
});
return obj;
})({
Name: "Some user",
Methods: {
Greeting: function () { return this.Name },
GetUserName: function() { console.log(this) }
},
});
然后,如果希望从SubObject引用较大的对象,可以将其转换为函数,并利用this
{
subObj: {
x:'hello_world';
}
}
我使用了Function()构造函数,因为它允许我将函数创建为字符串,这样我就可以将字符串作为代码传递
var tmpVal=reference_to_subObj; //keep value of subObj safe
reference_to_subObj=function(){return this;}//this returns the scope, here the parent
var parent=reference_to_subObj(); //call the function
reference_to_subObj=tmpVal; delete tmpVal; //set things back to normal
//Now you have variable 'parent'.
所以我可以调用findParent('obj.subObj')
就像@Quentin所说的,JS中没有向上的关系。不过试试这个解决方法
function findParent(stringReference) {
Function(/*same as above, except filled in all reference_to_subObj with stringReference.*/
//stringReference is a stringified version of dot or bracket notation.
这也类似于,
foo = { bar: {parent: foo} };
console.log(foo);
console.log(foo.bar.parent);
很晚才去参加聚会,但这样行得通
function Foo(){
this.bar = {parent: this}
}
foo = new Foo();
console.log(foo);
console.log(foo.bar.parent);
如果你将这个包装起来,可能会重复。在一个对象中显示问候语它不起作用。这一个有效,但为什么没有人投票给它?没有人尝试过这个?一点也不晚,谢谢!这是一个更准确的答案,而不是一个被接受的答案。被接受的答案只是抹去了问题,但这是一个正在回答的问题。谢谢你
foo = { bar: {parent: foo} };
console.log(foo);
console.log(foo.bar.parent);
function Foo(){
this.bar = {parent: this}
}
foo = new Foo();
console.log(foo);
console.log(foo.bar.parent);
var user = {
Name: "Some user",
Methods() {
return {
that: this,
ShowGreetings: function() {
console.log(this.that.Name)
},
GetUserName: function() { }
}
}
}
user.Methods().ShowGreetings() // Some user