gpt4 book ai didi

javascript - 从设置为原型(prototype)的对象调用属性

转载 作者:行者123 更新时间:2023-11-28 07:33:31 25 4
gpt4 key购买 nike

var Foo = function() {
this.message = "Hi";
}
Foo.prototype = {
say: {
hi: function() {
console.log(this.message);
}
}
}

[编辑]我知道 hi() 中的“this”指的是,有什么方法可以实现这一点吗?

var he = new Foo();
he.say.hi(); //"Hi" to console

最佳答案

您可以这样访问。

var Foo = function(){
this.par = 3;

this.sub = new(function(t){ //using virtual function to create sub object and pass parent object via 't'
this.p = t;
this.subFunction = function(){
alert(this.p.par);
}
})(this);
}

var myObj = new Foo();
myObj.sub.subFunction() // will popup 3;

myObj.par = 5;
myObj.sub.subFunction()

关于javascript - 从设置为原型(prototype)的对象调用属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28853843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com