javascript & call
1, Test call function of Array
var slice = Array.prototype.slice;
alert("slice.call(NaN).length:"+slice.call(NaN).length); // result : 0
alert("slice.call({0:'foo',length:'10',9:'2'})[0]:"+slice.call({0:'foo',length:'10',9:'2'})[0]);
// result : foo
alert("slice.call({0:'foo',length:'10',9:'2'})[9]:"+slice.call({0:'foo',length:'10',9:'2'})[9]);
// result : 2
alert("slice.call({0:'foo',length:'bar'})[0]:"+slice.call({0:'foo',length:'bar'})[0]); //result : undefined
So, function call(arg) , "arg" is Set of name/value pairs, set enclosed by a pair of curly braces{} , "length" is length of array, others are order number of array.
2, Mouse position
<script>
function mouseMove(ev)
{
ev= ev || window.event;
var mousePos = mouseCoords(ev);
//alert(ev.pageX);
document.getElementById('xxx').value = mousePos.x;
document.getElementById('yyy').value = mousePos.y;
}
function mouseCoords(ev)
{
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
document.onmousemove = mouseMove;
</script>
var slice = Array.prototype.slice;
alert("slice.call(NaN).length:"+slice.call(NaN).length); // result : 0
alert("slice.call({0:'foo',length:'10',9:'2'})[0]:"+slice.call({0:'foo',length:'10',9:'2'})[0]);
// result : foo
alert("slice.call({0:'foo',length:'10',9:'2'})[9]:"+slice.call({0:'foo',length:'10',9:'2'})[9]);
// result : 2
alert("slice.call({0:'foo',length:'bar'})[0]:"+slice.call({0:'foo',length:'bar'})[0]); //result : undefined
So, function call(arg) , "arg" is Set of name/value pairs, set enclosed by a pair of curly braces{} , "length" is length of array, others are order number of array.
2, Mouse position
<script>
function mouseMove(ev)
{
ev= ev || window.event;
var mousePos = mouseCoords(ev);
//alert(ev.pageX);
document.getElementById('xxx').value = mousePos.x;
document.getElementById('yyy').value = mousePos.y;
}
function mouseCoords(ev)
{
if(ev.pageX || ev.pageY){
return {x:ev.pageX, y:ev.pageY};
}
return {
x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
y:ev.clientY + document.body.scrollTop - document.body.clientTop
};
}
document.onmousemove = mouseMove;
</script>
评论
发表评论