Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对array或object使用for in 或for of循环时,会撞到自定义的方法 #1

Open
garywill opened this issue Jun 24, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@garywill
Copy link
Owner

garywill commented Jun 24, 2022

由于对Object创建了自定义方法,
for ( .. in ..)
for (.. of .. )
这两种循环总是会遍历到第一个自定义方法

cc-visualize/common.js

Lines 13 to 61 in 83f435f

Object.prototype.q$ = function(selectorStr) {
function handleNonArrayObject(obj)
{
return obj.querySelector(selectorStr);
}
var obj = this;
var result = null;
if ( HTMLElement.prototype.isPrototypeOf(obj) )
result = handleNonArrayObject( obj );
else if ( typeof(obj) != "string" &&
obj.length !== undefined && obj.length > 0
)
{
for( subObj of obj )
{
result = handleNonArrayObject(subObj);
if (result)
break;
}
}
return result;
}
Object.prototype.q$$ = function(selectorStr) {
function handleNonArrayObject(obj)
{
return obj.querySelectorAll(selectorStr);
}
var obj = this;
var result = [];
if ( HTMLElement.prototype.isPrototypeOf(obj) )
result = Array.from( handleNonArrayObject( obj ) );
else if ( typeof(obj) != "string" &&
obj.length !== undefined && obj.length > 0
)
{
for( subObj of obj )
{
result = result.concat ( Array.from (handleNonArrayObject(subObj) ) );
}
result = Array.from ( new Set(result) );
}
return result;
}

@garywill garywill added the bug Something isn't working label Jun 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant