[英]Javascript Prototype reuse same method
I have this JS file 我有这个JS文件
function APIAccess(){
this.LoadScreen = function(){
var loadScreen = $('#loadScreen');
if(loadScreen.html() == undefined){
loadScreen = '<div id="loadScreen" style="display: none;width: 100%; height: 100%; top: 0pt;left: 0pt;">' +
'<div id="loadScr" style="filter: alpha(opacity = 65); z-index: 9999;border: medium none; margin: 0pt; padding: 0pt; width: 100%; height: 100%; top: 0pt;left: 0pt; background-color: rgb(0, 0, 0); opacity: 0.2; cursor: wait; position: fixed;"></div>' +
'<div id="loader" style="z-index: 10000; position: fixed; padding: 0px; margin: 0px;width: 30%; top: 40%; left: 35%; text-align: center;cursor: wait; ">' +
'<img src="img/ajax-loader.gif" alt="loading" /></div></div>';
$(document.body).append(loadScreen);
}
};
this.APICall = function(url, params, method, callback){
this.LoadScreen();
var postData = null;
if(params != null){
postData = JSON.stringify(params);
}
if(url.toLowerCase().indexOf("http") < 0){
url = "http://" + url;
}
$('#loadScreen').show(function(){
$.ajax({
url: url,
async: true,
type: method,
data: postData,
success: function(data){
$('#loadScreen').hide();
callback(data);
},
error:function(data){
alert("failure");
return false;
}
});
});
};
}
function Domain(reqCallback){
this.url = 'http://beta.test123.net/api/domain';
this.params = null;
this.method = 'GET';
this.callback = function(data){
setCookie("domain", data);
if(typeof reqCallback != null){
reqCallback(data);
}
};
this.request = this.APICall(this.url, this.params, this.method, this.callback);
}
Domain.prototype = new APIAccess;
function Login(usermail, pass, reqCallback){
var domainUrl = getCookie("domain");
if(domainUrl == null)
return false;
else
domainUrl += '/api/login';
this.url = domainUrl;
this.params = {"email": usermail, "password": password};
this.method = 'POST';
this.callback = function(data){
setCookie("login", data);
if(typeof reqCallback != null){
reqCallback(data);
}
};
this.request = this.APICall(this.url, this.params, this.method, this.callback);
}
Login.prototype = new APIAccess;
If you see the method this.request = this.APICall(this.url, this.params, this.method, this.callback);
如果你看到方法
this.request = this.APICall(this.url, this.params, this.method, this.callback);
will be repeated everytime. 每次都会重复。 I wish it could be placed in APIAccess function.
我希望它可以放在APIAccess功能中。 can you please suggest what can be done.
你能告诉我们可以做些什么。
I use this in my html as this 我在我的HTML中使用这个
$(document).ready(function(){
var domain = new Domain(function(data){
alert(data);
});
domain.request;
});
I'm not sure about if this is what you are asking for, The most important part of the code below is that I have used Object.create() instead of object prototype to "inherit" from the base class If you want to learn why? 我不确定这是否是你要求的,下面代码中最重要的部分是我使用Object.create()代替对象原型从基类“继承”如果你想学习为什么? visit https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create
访问https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Object/create
hope it helps :) 希望能帮助到你 :)
//Base class
var APIAccess = {
LoadScreen : function () {
var loadScreen = $('#loadScreen');
if (loadScreen.html() == undefined) {
loadScreen = '<div id="loadScreen" style="display: none;width: 100%; height: 100%; top: 0pt;left: 0pt;">' +
'<div id="loadScr" style="filter: alpha(opacity = 65); z-index: 9999;border: medium none; margin: 0pt; padding: 0pt; width: 100%; height: 100%; top: 0pt;left: 0pt; background-color: rgb(0, 0, 0); opacity: 0.2; cursor: wait; position: fixed;"></div>' +
'<div id="loader" style="z-index: 10000; position: fixed; padding: 0px; margin: 0px;width: 30%; top: 40%; left: 35%; text-align: center;cursor: wait; ">' +
'<img src="img/ajax-loader.gif" alt="loading" /></div></div>';
$(document.body).append(loadScreen);
}
},
APICall : function (url, params, method, callback) {
this.LoadScreen();
var postData = null;
if (params != null) {
postData = JSON.stringify(params);
}
if (url.toLowerCase().indexOf("http") < 0) {
url = "http://" + url;
}
$('#loadScreen').show(function () {
$.ajax({
url: url,
async: true,
type: method,
data: postData,
success: function (data) {
$('#loadScreen').hide();
callback(data);
},
error: function (data) {
alert("failure");
return false;
}
});
});
},
//added to base class
url : null,
params : null,
method : null,
callback : null,
request : function(){
//TODO validate url, params and method here
APICall(this.url, this.params, this.method, this.callback);
}
}
var Domain = function(reqCallback) {
var obj = Object.create(APIAccess);
//obj.prototype = APIAccess;
obj.url = 'http://beta.test123.net/api/domain';
obj.params = null;
obj.method = 'GET';
obj.callback = function (data) {
setCookie("domain", data);
if (typeof reqCallback != null) {
reqCallback(data);
}
};
return obj;
}
var Login = function (usermail, password, reqCallback) {
var domainUrl = getCookie("domain");
if (domainUrl == null){
return false;
}
else{
domainUrl += '/api/login';
}
var obj = Object.create(APIAccess);
//obj.prototype = APIAccess;
obj.url = domainUrl;
obj.params = { "email": usermail, "password": password };
obj.method = 'POST';
obj.callback = function (data) {
setCookie("login", data);
if (typeof reqCallback != null) {
reqCallback(data);
}
}
return obj;
}
//Code below is just for testing
function getCookie(str){
return 'test';
}
console.log(
new Domain(function(data){alert(data);}), //domain
new Login( //loging
'user',
'passwd',
function(data){alert(data);}
)
)
OweRReLoaDeD's answer is correct, but to put it more succinctly: OweRReLoaDeD的答案是正确的,但更简洁地说:
You should not instantiate the base class just to setup inheritance. 您不应仅仅为了设置继承而实例化基类。
Domain.prototype = new APIAccess;
Should be 应该
Domain.prototype = Object.create(APIAccess);
Having said this, what you're modeling looks a little weird with inheritance, wish I had time to suggest a different way. 说完这个之后,你所建模的东西看起来有点奇怪,希望我有时间建议一种不同的方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.