提交 a142d777 编写于 作者: S sushuang

Add isInstance to avoid "instanceof" fail when cross scope.

上级 161903e0
......@@ -7,6 +7,7 @@
import {__DEV__} from '../config';
import * as zrUtil from 'zrender/src/core/util';
import {enableClassCheck} from '../util/clazz';
// id may be function name of Object, add a prefix to avoid this problem.
function generateNodeKey (id) {
......@@ -138,10 +139,10 @@ graphProto.addEdge = function (n1, n2, dataIndex) {
n2 = this.nodes[n2];
}
if (!(n1 instanceof Node)) {
if (!Node.isInstance(n1)) {
n1 = nodesMap[generateNodeKey(n1)];
}
if (!(n2 instanceof Node)) {
if (!Node.isInstance(n2)) {
n2 = nodesMap[generateNodeKey(n2)];
}
if (!n1 || !n2) {
......@@ -188,10 +189,10 @@ graphProto.getEdgeByIndex = function (dataIndex) {
* @return {module:echarts/data/Graph.Edge}
*/
graphProto.getEdge = function (n1, n2) {
if (n1 instanceof Node) {
if (Node.isInstance(n1)) {
n1 = n1.id;
}
if (n2 instanceof Node) {
if (Node.isInstance(n2)) {
n2 = n2.id;
}
......@@ -248,7 +249,7 @@ graphProto.eachEdge = function (cb, context) {
graphProto.breadthFirstTraverse = function (
cb, startNode, direction, context
) {
if (!(startNode instanceof Node)) {
if (!Node.isInstance(startNode)) {
startNode = this._nodesMap[generateNodeKey(startNode)];
}
if (!startNode) {
......@@ -516,4 +517,7 @@ zrUtil.mixin(Edge, createGraphDataProxyMixin('hostGraph', 'edgeData'));
Graph.Node = Node;
Graph.Edge = Edge;
enableClassCheck(Node);
enableClassCheck(Edge);
export default Graph;
\ No newline at end of file
......@@ -327,7 +327,7 @@ listProto.mapDimension = function (coordDim, idx) {
*/
listProto.initData = function (data, nameList, dimValueGetter) {
var notProvider = data instanceof Source || zrUtil.isArrayLike(data);
var notProvider = Source.isInstance(data) || zrUtil.isArrayLike(data);
if (notProvider) {
data = new DefaultDataProvider(data, this.dimensions.length);
}
......
import {createHashMap, isTypedArray} from 'zrender/src/core/util';
import {enableClassCheck} from '../util/clazz';
import {
SOURCE_FORMAT_ORIGINAL,
SERIES_LAYOUT_BY_COLUMN,
......@@ -124,4 +125,6 @@ Source.seriesDataToSource = function (data) {
});
};
enableClassCheck(Source);
export default Source;
\ No newline at end of file
......@@ -52,7 +52,7 @@ import {OTHER_DIMENSIONS} from './dimensionHelper';
*/
function completeDimensions(sysDims, source, opt) {
// ??? remove the compatible?
if (!(source instanceof Source)) {
if (!Source.isInstance(source)) {
source = Source.seriesDataToSource(source);
}
......
......@@ -19,7 +19,7 @@ import {
* If typed array used, chunk size must be fixed.
*/
export function DefaultDataProvider(source, dimSize) {
if (!(source instanceof Source)) {
if (!Source.isInstance(source)) {
source = Source.seriesDataToSource(source);
}
this._source = source;
......
......@@ -48,7 +48,7 @@ export {createSymbol} from './util/symbol';
*/
export function createScale(dataExtent, option) {
var axisModel = option;
if (!(option instanceof Model)) {
if (!Model.isInstance(option)) {
axisModel = new Model(option);
zrUtil.mixin(axisModel, axisModelCommonMixin);
}
......
......@@ -69,6 +69,26 @@ export function enableClassExtend(RootClass, mandatoryMethods) {
};
}
var classBase = 0;
/**
* Can not use instanceof, consider different scope by
* cross domain or es module import in ec extensions.
* Mount a method "isInstance()" to Clz.
*/
export function enableClassCheck(Clz) {
var classAttr = ['__\0is_clz', classBase++, Math.random().toFixed(3)].join('_');
Clz.prototype[classAttr] = true;
if (__DEV__) {
zrUtil.assert(!Clz.isInstance, 'The method "is" can not be defined.');
}
Clz.isInstance = function (obj) {
return !!(obj && obj[classAttr]);
};
}
// superCall should have class info, which can not be fetch from 'this'.
// Consider this case:
// class A has method f,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册