首先,我是Javascript的新手,所以这可能只是我的误解所以道歉。
我发现在调试我的Javascript时,this
的值会在我的原型方法中发生变化,并导致其他原型方法中出现异常。
它(this
)以参考该方法所属的对象开始,然后稍后更改为Window
,当我尝试引用方法this.request
时方法Clarizen.prototype.getSessionId
浏览器给出了以下错误:
未捕获的TypeError:this.request不是函数
我使用Chrome的调试器进行了检查,当this
进入此方法时,var Clarizen = function (userName, password, applicationId, optionalSessionId) {
if (userName === undefined) {
throw new Error('userName is missing');
}
if (password === undefined) {
throw new Error('password is missing');
}
this.userName = userName;
this.password = password;
this.applicationId = applicationId;
this.sessionId = (optionalSessionId === undefined) ? '' : optionalSessionId;
this.constants = ClarizenConstants.Urls;
this.jsonUser = null;
};
Clarizen.prototype.convertToObject = function(json)
{
console.log("convertToObject: " + json);
return JSON.parse(json);
};
Clarizen.prototype.debug = function (message) {
console.log("clarizen object debug " + message);
};
Clarizen.prototype.validateMethod = function (method) {
if (method === "GET" || method === "POST" || method === "HEAD") {
return true;
}
return false;
};
Clarizen.prototype.request = function (options) {
var url = (options.url !== null) ? options.url : null;
var callback = (typeof options.callback === "function") ? options.callback : null;
var method = (options.method !== null) ? options.method : "GET";
if (url === null)
{ throw new Error("url missing"); }
if (callback === null)
{ throw new Error("callback is missing/not a function type"); }
if (!Clarizen.prototype.validateMethod(options.method)) {
throw new Error("method can isnt supported currently " + method)
}
var util = new Bradaz();
var xhr = util.createXHR(url, method, true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var response = callback(xhr.responseText);
return response;
}
};
if (options.body !== null) {
xhr.send(options.body)
}
else {
xhr.send();
}
};
Clarizen.prototype.getSessionId = function(response)
{
var loginResponse = JSON.parse(response);
console.log("getSessionId " + loginResponse.serverLocation);
//The followin ERRORS
var r = this.request (
{
url: this.serverLocation + ClarizenConstants.Urls.authentication,
method: "POST",
callback: this.convertToObject,
body: this.jsonUser
}
);
};
Clarizen.prototype.login = function () {
var obj = new ClarizenObject();
var user = obj.factory({
type: 'User',
userName: "username",
password: "password"
});
var jsonBody = JSON.stringify(user);
this.jsonUser = jsonBody;
var response = this.request (
{
url: ClarizenConstants.Urls.getServerDefinition,
method: "POST",
callback: this.getSessionId,
body: jsonBody
}
);
};
的值会更改为Window。
以下是代码:
login
我们通过var clarizen = new Clarizen(user.userName, user.password);
clarizen.login();
原型方法(在pastebin中)输入对象:
mySelect.element(by.cssContainingText('option', "Terminology")).click();