lvleihere / code-segment Goto Github PK
View Code? Open in Web Editor NEWthis is mini-code-segment
this is mini-code-segment
chrome中有很多的方便的快捷键,这记录下
$
的使用
$0
你就能取到该节点,但前提是当前环境是纯净的~$0 === document.querySelector('title') // true
对应的 $1
取的是其直接的父级元素
$1 === document.body // true
如果说 $
是 document.querySelector
的简写,该方法选择的是某个具体单一的元素,那么对应的 $$
则是 document.querySelectotAll
外加 Array.from()
的简写
$i
该方法需要安装google插件 Console Importer ,用气使用npm包很方便的下载调试。
====end====
// one
const data = {
value: ''
};
const _data = {
value: ''
};
Object.defineProperty(data, 'value', {
get() {
return _data.value;
},
set(value) {
ipt.value = value;
_data.value = value;
console.log(data.value);
}
});
const ipt = document.querySelector('input');
ipt.addEventListener('input', function(e) {
data.value = e.target.value;
});
setTimeout(() => {
data.value = 3;
}, 3000);
// other
let data = {};
const _data = {};
data = new Proxy(data, {
get(target, prop, receive) {
return _data[prop];
},
set(target, prop, value, receive) {
ipt.value = value;
_data[prop] = value;
console.log(data.value);
}
});
// V -> M
const ipt = document.querySelector('input');
ipt.addEventListener('input', function(e) {
data.value = e.target.value;
});
// M -> V
setTimeout(() => {
data.value = 3;
}, 3000);
将时间戳20180106000000 -> 转成 2018-01-06 00:00:00 或者其他
// xxxx-xx-xx xx:xx:xx 的形式
const formatData = (str, formater) => {
const arr = str.split('');
let i = 0;
return formater.replace(/x/g, () => arr[i++]);
};
const str = '20180106000000';
const formater = 'xxxx-xx-xx xx:xx:xx';
const formatRes = formatData(str, formater);
console.log(formatRes); // 2018-01-06 00:00:00
// YY-MM-DD hh:mm:ss 的形式
const formatData = (str, formater) => {
const arr = str.split('');
let i = 0;
return formater.replace(/[YMDhms]/g, () => arr[i++]);
};
const str = '20180106000000';
// const formater = 'xxxx-xx-xx xx:xx:xx';
const formater = 'YY-MM-DD hh:mm:ss';
const formatRes = formatData(str, formater);
console.log(formatRes); // 2018-01-06 00:00:00
Function.prototype.before = function(beforeFn) {
const _this = this;
return function() {
beforeFn.call(this, arguments);
_this.call(this, arguments);
};
};
Function.prototype.after = function(afterFn) {
const _this = this;
return function() {
_this.call(this, arguments);
afterFn.call(this, arguments);
};
}
function originFn() {
console.log(1);
}
originFn
.before(function() {
console.log(2);
})
.after(function() {
console.log(3);
})();
简易深克隆, 没考虑Date、Regexp、Error
function
或者 undefined
, 你可以使用常规的 JSON.parse(JSON.stringify())
进行深克隆。/**
* 简易深度克隆
* @param {*} originVal - 原始数据
*/
const deepClone = originVal => {
if (typeof originVal !== 'object') {
return originVal;
}
const hasDeepCloned = originVal.constructor === Object ? {} : [];
for (const key in originVal) {
if (originVal.hasOwnProperty(key)) {
const val = originVal[key];
hasDeepCloned[key] = typeof val === 'object' ? deepClone(val) : val;
}
}
return hasDeepCloned;
}
const originData = {
name: 'thunder',
favor: [{
one: 'ball'
}]
};
const newData = deepClone(originData);
newData.name = 'modified data';
newData.favor[0].one = 'modified ball';
console.log(originData); // { name: 'thunder', favor: [ { one: 'ball' } ] }
console.log(newData); // { name: 'modified data', favor: [ { one: 'modified ball' } ] }
const getRandomColor = () => `#${(~~(Math.random() * (1<<24))).toString(16)}`;
/**
* 构建类之间的继承关系
*
* @param {Function} subClass 子类函数
* @param {Function} superClass 父类函数
*/
function inherits(subClass, superClass) {
var subClassProto = subClass.prototype;
var F = new Function();
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
extend(subClass.prototype, subClassProto);
}
/**
* 对象属性拷贝
*
* @param {Object} target 目标对象
* @param {Object} source 源对象
* @return {Object} 返回目标对象
*/
function extend(target, source) {
for (var key in source) {
if (source.hasOwnProperty(key)) {
var value = source[key];
if (typeof value !== 'undefined') {
target[key] = value;
}
}
}
return target;
}
由于需求,需要简便的运行C类代码,如下终端运行
gcc hello.c -o hello
运行产出:
./hello
g++ hello.c -o hello
运行产出:
./hello
clang -fobjc-arc -framework Foundation hello.m -o hello
运行产出:
./hello
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.