Giter Club home page Giter Club logo

codeblock's People

Contributors

ockor avatar

Watchers

 avatar  avatar

codeblock's Issues

浅复制与深复制

// 创建对象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高

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.