ockor / codeblock Goto Github PK
View Code? Open in Web Editor NEW代码学习片段
代码学习片段
// 创建对象a,a是栈,对象是堆并在内存生产新的存储空间
var a = {"a":1,"b":"hello world!","c":[1,2,3]};
// 创建对象b对对象a的引用
// b是栈,指针指向与a相同的堆,不在内存中生成新的存储空间
var b = a;
// 验证浅复制
a.b=2; // a->{"a":2,"b":"hello world!","c":[1,2,3]}
b; // b->{"a":2,"b":"hello world!","c":[1,2,3]}
// 深复制是对对象的枚举,通过查找最末一层值不为对象的属性,将该值赋值到新对象的同名属性上去。
// 对字符串或数字的赋值会开辟新内存,就避免了浅复制所带来的副作用。
// zepto 对复制的封装是一个很好的例子
function extend(target, source, deep) {
for (key in source)
if (deep && (isPlainObject(source[key]) || isArray(source[key]))) {
if (isPlainObject(source[key]) && !isPlainObject(target[key]))
target[key] = {}
if (isArray(source[key]) && !isArray(target[key]))
target[key] = []
extend(target[key], source[key], deep)
}
else if (source[key] !== undefined) target[key] = source[key]
}
// 对数组的深复制有两个快捷的方法:slice、concat
var b = a.slice(0); //slice: 33.480ms
var b = [].concat(a); //concat: 19.214ms
// 测试发现concat性能比slice高
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.