yangfan0095 / basis Goto Github PK
View Code? Open in Web Editor NEW掘金 知乎专栏文章 + 学习笔记 汇总 https://zhuanlan.zhihu.com/yangfan0095?author=hua-la-zi-mo-19
掘金 知乎专栏文章 + 学习笔记 汇总 https://zhuanlan.zhihu.com/yangfan0095?author=hua-la-zi-mo-19
不知道你现在找到工作了吗
我这边是阿里巴巴国际 UED 的前端开发,有兴趣聊一下吗
/**
* poll函数 用于执行轮询任务
* @param {*} fn 用于判断当前状态 是否满足执行回调条件
* @param {*} calback 满足条件后 执行的回调
* @param {*} errback 错误回调
* @param {*} timeout 超时
* @param {*} interval 默认轮询时间
*/
const poll = (fn, callback, errback, timeout = 10000, interval = 500) => {
const endTime = Number(new Date()) + timeout;
interval = interval || 100;
const __poll = () => {
if (fn()) {
callback();
} else if (Number(new Date()) < endTime) {
setTimeout(__poll, interval);
} else {
errback(new Error(`timed out for ${fn}: `));
}
};
__poll();
};
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。
你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
let twoSum = function(nums, target) {
let res = [];
for (let i = 0; i < nums.length; i++) {
let curTemp = isTarget(i, nums, target);
if (curTemp > -1) {
res.push(i);
res.push(curTemp);
break;
}
};
return res;
};
let isTarget = (index, arr, target) => {
let flag = -1;
for (let i = index + 1; i < arr.length; i++) {
if (arr[i] + arr[index] === target) {
flag = i;
break;
}
}
return flag;
}
let nums = [2, 7, 11, 15],
target = 9;
let result = twoSum(nums, target);
console.log(result);
简单 demo
const babylon = require("babylon");
const types = require("babel-types");
const visitors = {
File(node, scope) {
evaluate(node.program, scope);
},
Program(program, scope) {
for (const node of program.body) {
evaluate(node, scope);
}
},
ExpressionStatement(node, scope) {
return evaluate(node.expression, scope);
},
CallExpression(node, scope) {
// 获取调用者对象
const func = evaluate(node.callee, scope);
// 获取函数的参数
const funcArguments = node.arguments.map(arg => evaluate(arg, scope));
// 如果是获取属性的话: console.log
if (types.isMemberExpression(node.callee)) {
const object = evaluate(node.callee.object, scope);
return func.apply(object, funcArguments); // log.apply(console,'hello world')
}
},
MemberExpression(node, scope) {
const { object, property } = node;
// 找到对应的属性名
const propertyName = property.name;
// 找对对应的对象
const obj = evaluate(object, scope);
// 获取对应的值
const target = obj[propertyName];
// 返回这个值,如果这个值是function的话,那么应该绑定上下文this
return typeof target === "function" ? target.bind(obj) : target;
},
Identifier(node, scope) {
// 获取变量的值
return scope[node.name];
},
StringLiteral(node) {
return node.value;
}
};
function evaluate(node, scope) {
const _evalute = visitors[node.type];
if (!_evalute) {
throw new Error(`Unknown visitors of ${node.type}`);
}
// 递归调用
return _evalute(node, scope);
}
const code = "console.log('hello world')";
// 生成AST树
const ast = babylon.parse(code);
// 解析AST
// 需要传入执行上下文,否则找不到``console``对象
evaluate(ast, { console: console });
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.