gpt4 book ai didi

来自父类(super class)的 Javascript 方法

转载 作者:行者123 更新时间:2023-11-30 13:27:36 26 4
gpt4 key购买 nike

我是 javascript 的新手,所以 StackOverflow 上的一些现有答案让我感到困惑。

如果我在动态类中有一个静态类(如下所示)。如何访问动态类中的变量?

function ListItem() = {
var myItem = function() { return "something" };

...
ItemDropdown = function(){
show : function (){
//Want to access the ListItem's myItem here
alert(super.myItem() + "dropdown menu");
}
}
}


var foo = new ListItem();
foo.ItemDropdown.show();

最佳答案

如果你想让 ItemDropdown 成为 ListItem 的方法,你需要使用 this 声明它。

this.ItemDropdown = {

通过定义 myItem 的方式,您可以自行调用它。

alert(myItem() + "dropdown menu");

还有一些语法错误:

function ListItem() = {
// Should be
function ListItem() {

ItemDropdown = function(){
// Should be
this.ItemDropdown = {

所以,你最终得到的是:

function ListItem() {
var myItem = function() { return "something" };

this.ItemDropdown = {
show : function (){
alert(myItem() + "dropdown menu");
}
}
}

关于来自父类(super class)的 Javascript 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7948845/

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