[英]Javascript Chaining return undefined
i have a problem for getting this OOP works. 我有这个OOP作品的问题。 basically, this
$("notfound")
is not on document. 基本上,此
$("notfound")
不在文档中。 it put an error. 它放了一个错误。 but if change it to
$("parent")
it works because it is on document. 但是如果将其更改为
$("parent")
则可以正常运行,因为它在文档中。
check this fiddle : 检查这个小提琴:
https://jsfiddle.net/k6j70f1h/8/ https://jsfiddle.net/k6j70f1h/8/
in console.log the child is undefined. 在console.log中,子项未定义。
how to get this things works? 如何得到这个东西的作品?
what's wrong with my code? 我的代码有什么问题?
"use strict" var $, i; (function() { $ = function(el, context) { context = context || document; return new obj$(el, context); }; var obj$ = function(el, context) { var cl = context.getElementsByClassName(el), loop = cl.length; this.length = loop; for (i = 0; i < loop; i++) { this[i] = cl[i]; } }; obj$.prototype = { find : function(el) { if (this.length == 1) { return $( el, this[0] ); } }, css : function(obj, data) { if (this.length == 1) { this[0].style[obj] = data; return this; } } }; })(); var parent = $("notfound"), // this one cause error child = parent.find("child"); // throw an error child is undefined child.css("color", "orange"); parent.css("color", "purple");
<div class="parent">parent <div class="child">child</div> </div>
The line you've said is causing the error is not causing the error. 您所说的导致错误的行没有引起错误。
The line causing the error is: 导致错误的行是:
child.css("color", "orange");
It's causing the error because this line: 引起错误的原因是此行:
var parent = $("notfound"),
...returns a parent
object with length == 0
, and so this line: ...返回
length == 0
的parent
对象,因此此行:
child = parent.find("child"); // throw an error child is undefined
...calls find
on an object where this.length
is not 1
, so the code in find
doesn't go into the body of your if
statement and you don't return anything. ...电话
find
一个对象,其中上this.length
不是1
,所以在代码中find
不进入体内的的if
声明,你不返回任何东西。 That means calling parent.find(...)
results in undefined
, which you've assigned to child
. 这意味着调用
parent.find(...)
导致undefined
,您已将其分配给child
。 Thus, child.css(...)
is an attempt to call css
on undefined
. 因此,
child.css(...)
尝试在undefined
上调用css
。
If you want to make something jQuery-like, you'll want to add 如果您想制作类似jQuery的内容,则需要添加
return $();
...find parent.find
if this.length
is 0 (at a minimum): ...查找
parent.find
如果this.length
为0(至少):
find : function(el) {
if (this.length == 1) {
return $( el, this[0] );
}
return $();
}
Similarly, if you want to emulate jQuery, you'll always want to return this
from your css
function, not just if you have an element. 同样,如果您想模拟jQuery,则总是希望从
css
函数return this
值,而不仅仅是具有一个元素。
Here's an update with the minimum necessary changes: 这是具有最少必要更改的更新:
"use strict" var $, i; (function() { $ = function(el, context) { context = context || document; return new obj$(el, context); }; var obj$ = function(el, context) { var cl = context.getElementsByClassName(el), loop = cl.length; this.length = loop; for (i = 0; i < loop; i++) { this[i] = cl[i]; } }; obj$.prototype = { find : function(el) { if (this.length == 1) { return $( el, this[0] ); } return $(); // Added }, css : function(obj, data) { if (this.length == 1) { this[0].style[obj] = data; } return this; // Moved } }; })(); var parent = $("notfound"), // this one cause error child = parent.find("child"); // throw an error child is undefined child.css("color", "orange"); parent.css("color", "purple");
<div class="parent">parent <div class="child">child</div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.