$(document.body) 和 document.body 相同吗?课堂上清理垃圾和装订? - MooTools 1.3

发布于 2024-10-13 06:47:59 字数 1412 浏览 9 评论 0原文

我正在构建一个 MooTools 类,并且我的初始化函数中有这个:

this.css = null;

window.addEvent('domready', function(){

    this.document = $(document);
    this.body = $(document.body);
    this.head = $(document.head);

}.bind(this));

好的,现在回答问题...... 我应该在 init 中声明 this.css = null 或任何其他空变量吗:

this.css = null; // Maybe this.css = '' - empty string?

接下来是关于窗口和文档...我是否应该将其放入 $() 中,因为它是双向的,所以我只想知道哪个方式是首选?总结一下:

window.addEvent() // or should I use $(window).addEvent()
this.document = $(document) // or this.document = document
this.body = $(document.body) // or this.body = document.body

我将这些值存储到对象中以避免多次 DOM 查询,这样可以吗?或者每次都调用 $(selector) / $$(selector) 会更好吗?

还剩下两件事...关于绑定...每次都使用 .bind(this) 可以吗?还是使用 .bind(this.myDiv) 并在函数内部使用它会更好,例如: this.setStyle (...);而不是 this.myDiv.setStyle(...)

(function(){
  this.setStyle('overflow-y', 'visible');
 }.bind(this.body)).delay(100);

(function(){
  this.body.setStyle('overflow-y', 'visible');
 }.bind(this)).delay(100);

最后一件事是关于垃圾收集...我是否必须自己处理垃圾以及如何做到这一点(据我所知 MooTools 在卸载时自行处理) 。令人困惑的部分是我在 MT 文档中找到了函数:

myElement.destroy();

他们说:清空一个元素的所有子元素,删除并垃圾化该元素。对于在 pageUnload 之前清除内存很有用。

那我就得自己去扔垃圾吗?怎么做呢?何时使用.destroy()?我正在开发一个大型项目,我注意到 IE 在多次执行脚本时变得很慢(那么如何处理这个问题?可能需要一些清理,内存泄漏?)。

I am building a MooTools class and I have this in my initialize function:

this.css = null;

window.addEvent('domready', function(){

    this.document = $(document);
    this.body = $(document.body);
    this.head = $(document.head);

}.bind(this));

Ok and now to the questions ...
Should I declare this.css = null or any other empty variable in the init:

this.css = null; // Maybe this.css = '' - empty string?

Next thing is about window and document ... Should I put it into $() or not because it works both way, so I just want to know which way is preferred? So to summarize:

window.addEvent() // or should I use $(window).addEvent()
this.document = $(document) // or this.document = document
this.body = $(document.body) // or this.body = document.body

I stored these values into object to avoid multiple DOM queries, is this ok? Or would it be better to call $(selector) / $$(selector) every time?

Two more things left ... About binding ... Is it ok to use .bind(this) every time or would it be better to use .bind(this.myDiv) and use it inside function as eg.: this.setStyle(...); instead of this.myDiv.setStyle(...)

(function(){
  this.setStyle('overflow-y', 'visible');
 }.bind(this.body)).delay(100);

or

(function(){
  this.body.setStyle('overflow-y', 'visible');
 }.bind(this)).delay(100);

And the last thing is about garbage collection ... Do I have to garbage myself and how to do it (as far as I know MooTools does it on its own on unload). The confusing part is that I found function in MT docs:

myElement.destroy();

They say: Empties an Element of all its children, removes and garbages the Element. Useful to clear memory before the pageUnload.

So I have to garbage on my own? How to do that? When to use .destroy()? I am working on a huge project and I notice that IE gets slow over multiple executions of the script (so how to handle that? probably some cleaning needed, memory leaks?).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗笺 2024-10-20 06:47:59

pff,这个有点长。

首先,初始变量。 this.css = null...我设置“空”变量的唯一一次是:类型转换;当它是一个对象的属性时,我可以引用并且不希望未定义;当它是一个字符串时,我将连接它或一个数字,我将增加/减少; null 在这一点上并没有真正的用处。

编写 mootools 类时的常见/良好实践是使用 Options 类作为 mixin。这允许您使用可以在实例化时覆盖的默认设置来设置默认选项对象。类似地,Object.merge({ var: val}, useroptions); 可以覆盖默认值(如果提供)。

现在,iirc,有时您必须使用 $(document.body) ,这不是因为 document.body 不起作用,而是因为应用 $() > 也在 IE 中应用 Element 原型(因为 Element 原型没有在那里扩展,所以这些方法直接应用于元素,这在您 $ 它们时发生)。此外,$ 分配目标元素的内部 UID,并允许为该元素使用元素存储。我认为没有必要使用 $(document)$(window) - 默认情况下它们会根据需要进行“扩展”。无论如何,即使在 IE 中,您也只需 $(something) 一次,之后就可以继续将其用作“something”。检查我的 document.getElementById("foo").method() 示例 - 您可以单独运行 $("foo"); ,然后尝试 document.getElementById("foo").method() 再次 - 它也适用于 IE。

window.addEvent(); // is fine. 
document.body.adopt(new Element("div")); // not fine in IE.
new Element("div").inject(document.body); // fine.

并自行:

$(document.body).adopt(new Element("div")); // fine.
document.body.adopt(new Element("span")); // now fine, after first $.

在 ie8 中查看: http://www.jsfiddle.net/AePzD/1/ - 第一次尝试设置背景失败,但第二次尝试有效。随后,document.body.methods() 调用将正常工作。

http://www.jsfiddle.net/AePzD/2/ - 这显示了元素如何($ 也返回)可以在 webkit/mozilla 中有方法,但在 trident 中没有。但是,用 $("foo") 替换它,它将开始工作。经验法则: $ 元素在应用方法之前不会动态创建。

当然,存储选择器可能是一种良好的性能实践。但它也会用许多变量填充你的作用域链,所以要小心。如果您将使用选择器两次或多次,最好将其缓存。不这样做并不是什么戏剧性的事情,像 sizzle 和 slick 这样的选择器引擎如今速度如此之快,这并不重要(除非您当时正在制作动画并且它会影响您的 FPS)。

至于绑定,随你喜欢。

请记住延迟有第二个参数,BIND:

(function(){
      this.setStyle('background', 'blue');
 }).delay(100, $("foo"));

很多函数也是如此。这个特定的绑定不是很有用,但在类中,您可能想要执行

(function(){
      this.element.setStyle('background', 'blue');
      this.someMethod();
 }).delay(100, this));

GC。当然,mootools 有自己的 GC。然而,在我看来,.destroy 是一个非常好的做法。如果您不需要 DOM 中的某些内容,请使用 element.dispose()。如果您不会再次将其附加到 DOM,请使用 .destroy() - 删除所有子节点并进行清理。更多内存 \o/

关于 IE 的建议...狡猾。如果可以的话,您可以使用 drip 来跟踪内存泄漏,像 dynatrace 这样的东西可以很好地进行分析。在实践方面......确保你不使用内联js,你总是清理你不需要的东西(事件,元素),并且通常要小心,特别是当你堆积事件和处理ajax时(带来需要事件的新元素 - 考虑事件委托......)。使用更少的 dom 节点 - 也有帮助...

pff, this is a bit long.

first, initial variables. this.css = null... the only time i'd set 'empty' variables are: typecast; when it's a property of an object i may reference and don't want undefined; when it's a string i will concatenate with or a number i will incre/decrement; null is not really useful at this point.

a common / good practice when writing a mootools class is to use the Options class as a mixin. this allows you to set default options object with your default settings that can be overridden upon instantiation. similarly, Object.merge({ var: val}, useroptions); can override a default val if supplied.

now, iirc, there are times when you'd have to use $(document.body) and it's not because document.body does not work, it's because applying $() also applies Element prototypes in IE (since Element prototype is not extended there, the methods are applied to the elements directly instead, which happens when you $ them). Also, $ assigns an internal UID of the target element and allows for element storage to be used for that element. I don't see a point to using $(document) or $(window) - they are 'extended' as much as needed by default. In any case, even in IE, you only need to $(something) the one time and can continue using it as just 'something' afterwards. check my document.getElementById("foo").method() example - you can just run $("foo"); on it's own and then try document.getElementById("foo").method() again - it will work in IE too.

window.addEvent(); // is fine. 
document.body.adopt(new Element("div")); // not fine in IE.
new Element("div").inject(document.body); // fine.

and on their own:

$(document.body).adopt(new Element("div")); // fine.
document.body.adopt(new Element("span")); // now fine, after first $.

see this in ie8: http://www.jsfiddle.net/AePzD/1/ - first attempt to set the background fails but the second one works. subsequently, document.body.methods() calls are going to work fine.

http://www.jsfiddle.net/AePzD/2/ - this shows how the element (which $ also returns) can have methods in webkit/mozilla and not in trident. however, replace that with $("foo") and it will start working. rule of thumb: $ elements you don't dynamically create before applying methods to them.

storing selectors can be a good performance practice, for sure. but it can also fill your scope chain with many variables so be careful. if you will use a selector two or more times, it's good to cache it. failing to do so is not a drama, selector engines like sizzle and slick are so fast these days it does not matter (unless you are animating at the time and it impacts your FPS).

as for binding, whichever way you like.

keep in mind delay has a second argument, BIND:

(function(){
      this.setStyle('background', 'blue');
 }).delay(100, $("foo"));

so do quite a few functions. this particular bind is not very useful but in a class, you may want to do

(function(){
      this.element.setStyle('background', 'blue');
      this.someMethod();
 }).delay(100, this));

GC. mootools does it's own GC, sure. however, .destroy is a very good practice, imo. if you don't need something in the DOM, use element.dispose(). if you won't attach it to the DOM again, use .destroy() - removes all child nodes and cleans up. more memory \o/

advice on IE... dodgy. you can use drip if you can to trace memory leaks, there are things like dynatrace that can be very good in profiling. in terms of practices... make sure you don't use inline js, you always clean up what you don't need (events, elements) and generally, be careful, esp when you are stacking up events and dealing with ajax (bring new elements that need events - consider event delegation instead...). use fewer dom nodes - also helps...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文