gpt4 book ai didi

javascript - 函数调用不起作用

转载 作者:行者123 更新时间:2023-11-28 15:09:38 26 4
gpt4 key购买 nike

我有以下代码,

(function(exports) {
"use strict";

var Common = function() {
this.loading = function(type){
this.type();
this.show = function(){
alert('show');
}

this.hide = function(){
alert('hide');
}
}
exports.Common = Common;
exports.Common = new Common();
}(window));

我试图访问 show()hide() 就像,

   Common.loading('show');
Common.loading('hide');

但是它抛出一个错误,

TypeError: this.type is not a function

最佳答案

你正在传递一个字符串,当它到达方法时它不会神奇地变成一个函数:)

您正在尝试访问属于 this 属性的方法,因此请替换

this.type();

this[type]();

关于javascript - 函数调用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37113216/

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