gpt4 book ai didi

Javascript 类返回未定义

转载 作者:行者123 更新时间:2023-12-01 01:31:13 24 4
gpt4 key购买 nike

使用此代码时,函数返回未定义而不是对象。

function Oauth_User_Profile() {
this.username = "=";
this.password = "=";
this.refresh_token = "=";
this.access_token = "=";
this.request = function(url, formData, method, then) {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if(xhttp.readyState == 4 && xhttp.status == 200)
{
var j = JSON.parse(this.responseText);
then(j);
}
}
xhttp.open(method, url);
xhttp.send(JSON.stringify(formData));
}
}

var oauth_user_profile = Oauth_User_Profile();
oauth_user_profile.username = "sample";

我为其他类提供了几乎相同的代码,运行良好,所以我不知道为什么它不能正常运行

重复:问题被标记为 [ How do I return the response from an asynchronous call? 的重复项。但是,我不想返回调用的结果,我想返回一个包含用户名、密码、refresh_token...以及可以运行的方法的对象。删除异步部分时,我遇到了完全相同的问题:

function Oauth_User_Profile() {
this.username = "=";
this.password = "=";
this.refresh_token = "=";
this.access_token = "=";
}

这仍然给出错误:未捕获类型错误:无法设置未定义的属性“用户名” 在 testm.html:67

最佳答案

您缺少 new 关键字:

var oauth_user_profile = new Oauth_User_Profile();

关于Javascript 类返回未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53258373/

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