请考虑Mozilla Docs关于JavaScript内存泄漏的这句话:
Run Code Online (Sandbox Code Playgroud)function addHandler() { var el = document.getElementById('el'); el.onclick = function() { this.style.backgroundColor = 'red'; } }上面的代码将元素设置为在单击时变为红色.它还会造成内存泄漏.为什么?因为对el的引用无意中被捕获到为匿名内部函数创建的闭包中.这将在JavaScript对象(函数)和本机对象(el)之间创建循环引用.
请以简单明了的方式解释上述泄漏的原因,我没有得到确切的观点.
由于泄漏,网站/页面是否面临安全问题?我该如何避免它们?其他什么代码会导致内存泄漏?如何判断内存泄漏的时间?
我是内存泄漏主题的绝对新手.有人可以一步一步地为我澄清这些东西吗?还有人可以帮我澄清这句话"这会在JavaScript对象(函数)和本机对象(el)之间创建一个循环引用."
javascript internet-explorer garbage-collection memory-leaks circular-reference
JavaScript声明和未声明变量之间的主要区别是什么,因为delete运算符不适用于声明的变量?
var y = 43; // declares a new variable
x = 42;
delete x; // returns true (x is a property of the global object and can be deleted)
delete y; // returns false (delete doesn't affect variable names)
Run Code Online (Sandbox Code Playgroud)
为什么会这样?全局声明的变量也是窗口对象的属性,为什么不能删除它?
如何知道对象是否是数组?
var x=[];
console.log(typeof x);//output:"object"
alert(x);//output:[object Object]
console.log(x.valueOf())//output:<blank>? what is the reason here?
console.log([].toString()); also outputs <blank>
Object.prototype.toString.call(x) output:[object Array] how?
Run Code Online (Sandbox Code Playgroud)
自console.log([].toString()); 输出:空白
第一名:
为什么我在最后的第二个陈述中空白?
第二:
有没有办法确切知道对象是什么:数组或普通对象({})没有x.join()等各自方法的帮助,表明x是一个数组,而不是这种方式.
实际上,在jquery选择中,如$("p")返回jquery对象,所以如果我使用
console.log(typeof $("p"));//output:"object
Run Code Online (Sandbox Code Playgroud)
我只是想知道对象的实际名称.请它.谢谢你的帮助
使用[]和.访问数组或对象属性的真正区别是什么?哪一个使用?
另外为什么.运营商不允许索引属性?
第一部分:
因为当您添加两个数组时,一切都按预期工作:
[] + [] //output:''
Run Code Online (Sandbox Code Playgroud)
添加数组和对象也符合我们的期望:
[] + {}
output:'[object Object]'
Run Code Online (Sandbox Code Playgroud)
{} + {}在JavaScript中NaN?
这是意料之外的结果,这背后的原因是什么?
第二部分:
在没有前缀0的字符串比较中,3大于12:
"3" > "12"
: true
Run Code Online (Sandbox Code Playgroud)
使用填充,一切正常:
"03" > "12"
: false
Run Code Online (Sandbox Code Playgroud)
字符串比较必须使用前缀0吗?添加前缀0的原因是什么?
Jquery.each()和Array.prototype.forEach()方法之间有什么区别,因为array.forEach()方法也可以用于循环具有length属性的类数组对象.我看到的唯一区别是参数的放置,还有什么区别呢?
I found this:
var obj = { one:1, two:2, three:3, four:4, five:5 };
jQuery.each(obj, function(i, val) {
$("#" + i).append(document.createTextNode(" - " + val));
});
Run Code Online (Sandbox Code Playgroud)
我想知道的是,jquery.each()是否为没有length属性的对象调用函数?
除以0给出了这个特殊值:
3/0 output:Infinity
Run Code Online (Sandbox Code Playgroud)
你不能互相发挥积极和消极的无限:
Infinity - Infinity output:NaN (Why?)
Run Code Online (Sandbox Code Playgroud)
事实证明,"超越无限"仍然是无限的:
Infinity + Infinity output:Infinity(this is accepted)
5 * Infinity
Infinity(this is also accepted)
Run Code Online (Sandbox Code Playgroud)
那么为什么无限无穷大对NaN的评价呢?应该是无穷大不是吗?我也想知道为什么不能将对象转换为原始值?抱歉一次发两个问题,因为这是我可以发布的最后一个问题.看这里:
var obj = {
valueOf: function () {
console.log("valueOf");
return {}; // not a primitive
},
toString: function () {
console.log("toString");
return {}; // not a primitive
}
}
Number(obj) //TypeError: Cannot convert object to primitive values
Run Code Online (Sandbox Code Playgroud) var o, d;
o = { get foo() { return 17; } };
d = Object.getOwnPropertyDescriptor(o, "foo");
// d is { configurable: true, enumerable: true, get: /*the getter function*/, set: undefined }
Run Code Online (Sandbox Code Playgroud)
那是什么get物体内呢?这是方法或财产还是其他什么?它如何工作或如何设置属性或方法来对象?如果我简单地忽略使用get和,我会遇到麻烦set吗?使用时是否有更多的优势,get而set不是简单地定义属性而没有使用.如果有的话,这些优势是什么.该.getOwnPropertyDescriptor()方法还会返回什么?如果它返回对象,我可以简单returnedobj.configurable地访问可配置的属性值吗?
根据Jquery API文档:
.position()返回: 对象
说明: 获取匹配元素集中第一个元素相对于偏移父元素的当前坐标。
此方法不接受任何参数。
但我发现使用此:
$("#position1").position({
my: "center",
at: "center",
of: "#targetElement"
});
Run Code Online (Sandbox Code Playgroud)
一个对象已经传递给position方法。这不是针对API文档吗?似乎传递给上述对象的属性有一些特殊含义。这些属性说明什么。它们做什么?我是jquery的完整入门。也许我错了 。
[].valueOf() 方法返回数组本身.据此
document.write([["a"]],["b"])
Run Code Online (Sandbox Code Playgroud)
应该返回['a'] b不是吗?但这不会发生,它只是写道ab.我只是想知道背后的原因.
对于字符串元素.toString()方法返回此信息,
["a","b"].toString()//a,b
Run Code Online (Sandbox Code Playgroud)
但是对于带有数组的元素,它应该返回
[["a"],"b"].toString()//[a],b
Run Code Online (Sandbox Code Playgroud)