3 回答

TA貢獻1797條經驗 獲得超4個贊
我也在這看了半天沒弄明白
現在大概懂了
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
fruits傳到myArray
在這個函數里myArray.constructor.toString()的結果就是
function Array() { [native code] }
indexof(Array)如果在function Array() { [native code] }中存在就會返回一個正數,大于-1;
如果在function Array() { [native code] }中不存在那么會返回-1,-1不大于-1所以返回false

TA貢獻1796條經驗 獲得超10個贊
myArray.constructor.toString() 是字符串function Array() { [native code] },
function Array() { [native code] }中Array在function這8個字母后面,從0(f為0)編號,一直到7,空格算1個,所以Array從第9個字符開始出現,所以 myArray.constructor.toString().indexOf("Array") 輸出數字為9。你可以嘗試把 myArray.constructor.toString().indexOf("function") >-1或者 myArray.constructor.toString().indexOf("function") >0 嘗試一下,或者把Array換成native試試

TA貢獻1824條經驗 獲得超5個贊
在本例中,我們將在 "Hello world!" 字符串內進行不同的檢索:
<script type="text/javascript">
var str="Hello world!"
document.write(str.indexOf("Hello") + "<br />")
document.write(str.indexOf("World") + "<br />")
document.write(str.indexOf("world"))
</script>
以上代碼的輸出:
0
-1
6
添加回答
舉報