Javascript 对象的属性和此

Javascript 对象的属性和此,javascript,Javascript,我想创建一个对象来存储我将在web应用程序中使用的变量 我无法使用this从uriGetToken访问clientId和clientSecret 我还可以使用token中的mApiGetToken中的函数 你能告诉我我做错了什么以及如何修复它吗 $(document).ready(function () { // General Settings var mApiSettings = { clientId: 'aaa',

我想创建一个对象来存储我将在web应用程序中使用的变量

我无法使用
this
uriGetToken
访问
clientId
clientSecret

我还可以使用
token
中的
mApiGetToken
中的函数

你能告诉我我做错了什么以及如何修复它吗

   $(document).ready(function () {

        // General Settings
        var mApiSettings = {
            clientId: 'aaa',
            clientSecret: 'bbb',
            token: mApiGetToken(),
            uriGetToken: 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + this.clientId + '&client_secret=' + this.clientSecret
        }

        console.log(mApiSettings.uriGetToken);
        // Get Autheticated, it requires getting a Token from HollyByte
        function mApiGetToken() {

            $.getJSON(mApiSettings.uriGetToken, processData);
            function processData(data) {
                mApiSettings.token = data.access_token;
            }
            //return token;
        }

        // For Testing
        console.log(mApiGetToken());

    });

的值是为调用该函数时出现的函数确定的

您正在使用的
this
的对象文本与
this
的值之间没有关联

<>还没有访问对象对象的属性的方法,该对象在创建它的对象文字语句的中间。< /P> 您需要使用变量

尽管您的示例中没有任何特殊字符,但您应该习惯于转义要插入到URI中的任何数据

    var clientId, clientSecret, mApiSettings;
    clientId = 'aaa';
    clientSecret = 'bbb';
    mApiSettings = {
        clientId: clientId,
        clientSecret: clientSecret,
        token: mApiGetToken(),
        uriGetToken: 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + encodeURIComponent(clientId) + '&client_secret=' + encodeURIComponent(clientSecret)
    }

的值是为调用该函数时出现的函数确定的

您正在使用的
this
的对象文本与
this
的值之间没有关联

<>还没有访问对象对象的属性的方法,该对象在创建它的对象文字语句的中间。< /P> 您需要使用变量

尽管您的示例中没有任何特殊字符,但您应该习惯于转义要插入到URI中的任何数据

    var clientId, clientSecret, mApiSettings;
    clientId = 'aaa';
    clientSecret = 'bbb';
    mApiSettings = {
        clientId: clientId,
        clientSecret: clientSecret,
        token: mApiGetToken(),
        uriGetToken: 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + encodeURIComponent(clientId) + '&client_secret=' + encodeURIComponent(clientSecret)
    }

这是一个常见的JavaScript问题,因为<代码>这个关键字不象其他的OOP语言,如java或C++。 问题是:

var o = {
    a : 2,
    b : this.a *2
}
console.log( b ); //prints NaN because the value for this.a is undefined at the time b is being initialized
因为
在对象文本初始化中不可访问。一种解决方法是使用对象的名称,而不是使用
this

var o = {
    a : 2,
    b : o.a *2
}
console.log( b ); //prints 4
或者,您可以一次定义一个对象:

var o = {
    a : 2
}
o.b = o.a *2;
console.log( b ); //prints 4

不管怎样,如果将
mApiSettings
更改为:

var mApiSettings = {
            clientId: 'aaa',
            clientSecret: 'bbb',
            token: mApiGetToken()
}

mApiSettings.uriGetToken = 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + mApiSettings.clientId + '&client_secret=' + mApiSettings.clientSecret;
您可以在此处阅读有关Javascript
this
关键字的更多信息:

编辑:

正如另一个答案所建议的,在将clientSecret和clientId嵌入URL之前,您可能需要对它们进行编码。如果是这种情况,您可以使用以下代码:

var mApiSettings = {
            clientId: 'aaa',
            clientSecret: 'bbb',
            token: mApiGetToken()
}

mApiSettings.uriGetToken = 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + encodeURIComponent( mApiSettings.clientId ) + '&client_secret=' + encodeURIComponent( mApiSettings.clientSecret );

这是一个常见的JavaScript问题,因为<代码>这个关键字不象其他的OOP语言,如java或C++。 问题是:

var o = {
    a : 2,
    b : this.a *2
}
console.log( b ); //prints NaN because the value for this.a is undefined at the time b is being initialized
因为
在对象文本初始化中不可访问。一种解决方法是使用对象的名称,而不是使用
this

var o = {
    a : 2,
    b : o.a *2
}
console.log( b ); //prints 4
或者,您可以一次定义一个对象:

var o = {
    a : 2
}
o.b = o.a *2;
console.log( b ); //prints 4

不管怎样,如果将
mApiSettings
更改为:

var mApiSettings = {
            clientId: 'aaa',
            clientSecret: 'bbb',
            token: mApiGetToken()
}

mApiSettings.uriGetToken = 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + mApiSettings.clientId + '&client_secret=' + mApiSettings.clientSecret;
您可以在此处阅读有关Javascript
this
关键字的更多信息:

编辑:

正如另一个答案所建议的,在将clientSecret和clientId嵌入URL之前,您可能需要对它们进行编码。如果是这种情况,您可以使用以下代码:

var mApiSettings = {
            clientId: 'aaa',
            clientSecret: 'bbb',
            token: mApiGetToken()
}

mApiSettings.uriGetToken = 'https://ccc/oauth/token?grant_type=client_credentials&client_id=' + encodeURIComponent( mApiSettings.clientId ) + '&client_secret=' + encodeURIComponent( mApiSettings.clientSecret );

抱歉,您的链接可能重复,但它与我的问题无关,我在一个属性中对此有问题。我刚刚注意到,虽然该问题确实回答了您的问题,但您尚未遇到该问题。您不必定义额外的一对变量。我已经修复了我答案中代码的错误。抱歉,您的链接可能重复,但它与我的问题无关,我在一个属性中对此有问题。我刚刚注意到,虽然问题确实回答了您的问题,但您尚未遇到该问题。您不必定义额外的一对变量。我已经修复了答案中代码的错误。“不管怎样,如果您更改了代码,您的代码应该可以工作…”-不,不应该。它将给出
Uncaught TypeError:无法读取未定义的属性“clientId”
,因为对象文本在创建之前未分配给
mApiSettings
。“无论如何,如果更改,代码应该可以工作…”-不,不应该。它将给出
Uncaught TypeError:无法读取未定义的属性“clientId”
,因为对象文本在创建之前未分配给
mApiSettings
。感谢您上次的编辑,我可以问您为什么在URL中转义字符吗?谢谢,因为如果数据包含特殊字符(如
&
),则无法正确传递。感谢您上次的编辑,我可以问您在URL中转义字符的原因吗?谢谢,因为如果数据包含特殊字符(如
&
),则无法正确传递。