//给数组添加一个find(v)函数,如果数组内有v,返回其下标,如果没有则返回-1(核心思路是使用this)
//此例中找到2下标为1,未找到4返回了-1
<script>
var arr = [];
arr[0] = 1;
arr[1] = 2;
arr[2] = 3;
function find(v){
for(var i=0; i<this.length; i++){
if(this[i] == v)
return i;
}
return -1;
}
Array.prototype.find = find; //添加成员函数
document.write(arr.find(2) + " " + arr.find(4));
</script>
//call()函数,输出为:
什么鬼
小白什么鬼
<script>
var dog={
name: "小白"
};
function test(){
document.write(this.name + "什么鬼<br/>");
}
test();
test.call(dog); //相当于dog.test();
</script>
//遍历对象的所有属性,像数组一样
<script>
var dog = {
a: 1,
b: 2,
c: 3
};
document.write("dog的属性下标和值<br/>");
for(var key1 in dog)
document.write(key1 + ":" + dog[key1] + "<br/>");
document.write("<br/>history对象的属性下标和值<br/><br/>");
for(var key2 in history)
document.write(key2 + ":" + history[key2] + "<br/>");
document.write("<br/>window对象的属性下标和值<br/><br/>");
for(var key3 in window)
document.write(key3 + ":" + window[key3] + "<br/>");
</script>
输出如下:
dog的属性下标和值
a:1
b:2
c:3
history对象的属性下标和值
length:1
back: function back() { [native code] }
forward: function forward() { [native code] }
go: function go() { [native code] }
window对象的属性下标和值
dog:[object Object]
key1:c
key2:go
key3:key3
clientInformation:[object Navigator]
clipboardData:[object DataTransfer]
closed:false
defaultStatus:
event:null
external:undefined
maxConnectionsPerServer:10
offscreenBuffering:auto
onfocusin:null
onfocusout:null
onhelp:null
onmouseenter:null
onmouseleave:null
screenLeft:175
screenTop:144
status:
document:[object Document]
frameElement:null
frames:[object Window]
history:[object History]
innerHeight:743
innerWidth:959
length:0
location:file:///D:/MyEclipse/Workspace/.metadata/.plugins/com.genuitec.eclipse.webdesigner3/JS_-1973688999_preview.html
name:
navigator:[object Navigator]
onabort:null
onafterprint:null
onbeforeprint:null
onbeforeunload:null
onblur:null
oncanplay:null
oncanplaythrough:null
onchange:null
onclick:null
oncontextmenu:null
ondblclick:null
ondrag:null
ondragend:null
ondragenter:null
ondragleave:null
ondragover:null
ondragstart:null
ondrop:null
ondurationchange:null
onemptied:null
onended:null
onerror:null
onfocus:null
onhashchange:null
oninput:null
onkeydown:null
onkeypress:null
onkeyup:null
onload:null
onloadeddata:null
onloadedmetadata:null
onloadstart:null
onmessage:null
onmousedown:null
onmousemove:null
onmouseout:null
onmouseover:null
onmouseup:null
onmousewheel:null
onoffline:null
ononline:null
onpause:null
onplay:null
onplaying:null
onprogress:null
onratechange:null
onreadystatechange:null
onreset:null
onresize:null
onscroll:null
onseeked:null
onseeking:null
onselect:null
onstalled:null
onstorage:null
onsubmit:null
onsuspend:null
ontimeupdate:null
onunload:null
onvolumechange:null
onwaiting:null
opener:undefined
outerHeight:952
outerWidth:1328
pageXOffset:0
pageYOffset:0
parent:[object Window]
performance:[object Performance]
screen:[object Screen]
screenX:-178
screenY:2
self:[object Window]
styleMedia:[object StyleMedia]
top:[object Window]
window:[object Window]
localStorage:undefined
sessionStorage:undefined
addEventListener: function addEventListener() { [native code] }
dispatchEvent: function dispatchEvent() { [native code] }
removeEventListener: function removeEventListener() { [native code] }
attachEvent: function attachEvent() { [native code] }
detachEvent: function detachEvent() { [native code] }
createPopup: function createPopup() { [native code] }
execScript: function execScript() { [native code] }
item: function item() { [native code] }
moveBy: function moveBy() { [native code] }
moveTo: function moveTo() { [native code] }
msWriteProfilerMark: function msWriteProfilerMark() { [native code] }
navigate: function navigate() { [native code] }
resizeBy: function resizeBy() { [native code] }
resizeTo: function resizeTo() { [native code] }
showHelp: function showHelp() { [native code] }
showModelessDialog: function showModelessDialog() { [native code] }
toStaticHTML: function toStaticHTML() { [native code] }
alert: function alert() { [native code] }
blur: function blur() { [native code] }
close: function close() { [native code] }
confirm: function confirm() { [native code] }
focus: function focus() { [native code] }
getComputedStyle: function getComputedStyle() { [native code] }
getSelection: function getSelection() { [native code] }
open: function open() { [native code] }
postMessage: function postMessage() { [native code] }
print: function print() { [native code] }
prompt: function prompt() { [native code] }
scroll: function scroll() { [native code] }
scrollBy: function scrollBy() { [native code] }
scrollTo: function scrollTo() { [native code] }
showModalDialog: function showModalDialog() { [native code] }
toString: function toString() { [native code] }
clearInterval: function clearInterval() { [native code] }
clearTimeout: function clearTimeout() { [native code] }
setInterval: function setInterval() { [native code] }
setTimeout: function setTimeout() { [native code] }