Skip to content

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

Description

@garywill

由于对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;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions