Javascript 试图理解';这';内部Prorotype和Lambda函数
所以今天我编写了一个AJAX对象 我创建了一个构造函数ajaxObj:Javascript 试图理解';这';内部Prorotype和Lambda函数,javascript,ajax,ecmascript-5,Javascript,Ajax,Ecmascript 5,所以今天我编写了一个AJAX对象 我创建了一个构造函数ajaxObj: function ajaxObj( arg1, arg2, wrapper ) { this.form = arg1; this.btn = arg2; this.msg = "Update successful"; this.url = "process.php"; this.w
function ajaxObj( arg1, arg2, wrapper ) {
this.form = arg1;
this.btn = arg2;
this.msg = "Update successful";
this.url = "process.php";
this.wrap = wrapper; // this is the div the callback uses
this.serial = null; // serialized form for POSTing
this.callback = function () {
var div = document.createElement("div");
div.innerHTML = this.msg;
div.setAttribute( "id", "desiredID" );
this.wrap.appendChild(div);
}
this.btn.onclick = initXHR;
}
在给定页面上实例化了多个ajaxObj类型的对象。我想包括那些不会改变并且应该在原型上共享的功能:
ajaxObj.prototype = {
constructor: ajaxObj,
makeXHR: function () {
// cross browser XHR code returns XHR obj
}
makeSer: function () {
this.serial = $(this.form).serialize();
}
initXHR: function () {
// this is where shit gets weird!
this.makeSer(); // makeSer function doesnt exit
var http = this.makeXHR(); // makeXHR doesn't exist
http.onreadystatechange = function () {
/* this function checked for
http.status / http.readyState
and attempted to call this.callback()
which of course didn't exist.
I also tried to change instance
properties here which did not work */
}
http.open( "POST", this.url, true ); /* this.url did not work
and had to be replaced
with "process.php" */
http.setRequestHeaders("Content-Type","application/x..."); // TL;DT
http.send( this.serial ) // <--- this works just fine???
}
与
。。。。请不要让我困惑。我的印象是我已经弄明白了。当我使用var关键字时,在构造函数中设置this=this似乎不起作用,而且很明显(javascript中的情况并不总是那么明显)删除了var关键字集,该关键字集等于用最近的对象实例实例化的值。请看一看。可能您调用的函数“错误”,即不在ajaxObj
实例上。给我们看一下那个调用
this.serial
起作用而this.url
不起作用的原因是您刚刚明确地分配给了this.serial
,尽管它可能不是您期望的对象的属性。只要浏览一下您的代码,我就可以看到只有在单击目标按钮时才会调用initXHR函数开火。如果您在initXHR中检查此,我相信您会发现它是您的按钮
我认为基于你目前的课堂设计,你需要使用旧的
var self = this;
在构造函数中,并将initXHR设置为特权函数(在构造函数中),以便访问它。我对下面的代码进行了注释,以向您展示我添加到您的ctor中的内容
function ajaxObj( arg1, arg2, wrapper ) {
// Assign this -> self at the start of the func
var self = this;
this.form = arg1;
this.btn = arg2;
this.msg = "Update successful";
this.url = "process.php";
this.wrap = wrapper;
this.serial = null;
this.callback = function () {
var div = document.createElement("div");
div.innerHTML = this.msg;
div.setAttribute( "id", "desiredID" );
this.wrap.appendChild(div);
}
// Make initXHR a privileged function, ie. one that has
// access to private variables in ctor
this.initXHR = function () {
self.url // should be accessible here
}
this.btn.onclick = this.initXHR; // Assign it.
}
您的右侧-而this.serial似乎不适用于页面上的所有表单(共有6个表单)。MDN是我的主页。我已经读了很多关于这个关键词的文章,并且进行了我自己的实验来尝试理解它,但我会再读一遍那个篇文章…谢谢,我确信我已经尝试过了。。。只是我把initXHR留在了原型中,而不是放在构造函数中。这正是我想做的。顺便说一句,完全脱离主题,我刚刚查看了Brendan Eich的网站,JavaScript的未来似乎有一些很酷的东西:)我很高兴这有帮助。我会对他的网站嗤之以鼻的,干杯。
var self = this;
function ajaxObj( arg1, arg2, wrapper ) {
// Assign this -> self at the start of the func
var self = this;
this.form = arg1;
this.btn = arg2;
this.msg = "Update successful";
this.url = "process.php";
this.wrap = wrapper;
this.serial = null;
this.callback = function () {
var div = document.createElement("div");
div.innerHTML = this.msg;
div.setAttribute( "id", "desiredID" );
this.wrap.appendChild(div);
}
// Make initXHR a privileged function, ie. one that has
// access to private variables in ctor
this.initXHR = function () {
self.url // should be accessible here
}
this.btn.onclick = this.initXHR; // Assign it.
}