threegeese / web Goto Github PK
View Code? Open in Web Editor NEWHome Page: https://threegeese.github.io/WEB/
Home Page: https://threegeese.github.io/WEB/
arguments
对象转成数组var args = Array.prototype.slice.call(arguments);
var args = [].slice.call(arguments);
// ES2015
const args = Array.from(arguments);
const args = [...arguments];
trim()
方法function trim(string) {
return string.replace(/^\s+|\s+$/g, '');
}
console.log(s[0])
会打印出什么?
var s = [];
var arr = s;
for(var i=0; i<3; i++) {
var pusher = {
value: "item" + i
},tmp;
if(i !== 2) {
tmp = []
pusher.children = tmp
}
arr.push(pusher)
arr = tmp
}
console.log(s[0])
先看结果:
{
value: "item0", children: [ { value: "item1", children:[ {value: "item2"} ] } ]
}
分析:
// i = 0
arr = s = [] // arr 和 s 指向同一个空数组
i = 0
tmp = [] // 为了区分用 tmp0 来表示
pusher = { value: "item0", children: tmp } // pusher0
arr = s = pusher
arr = tmp // 这里表明在 i = 1 时 arr 将持有 tmp0 的引用
// i = 1
s = { value: "item0", children: tmp } // 这里的 children 就是指向上面的 tmp0
tmp = [] // 这里的 tmp 用 tmp1 表示
pusher = { value: "item1", children: tmp } // pusher1
arr.push(puhser) // 意味着往 tmp0 里添加
arr = tmp1 // 上面 arr 指向 tmp0 这里将 arr 指向新的 tmp1
// i = 2
s = { value: "item0", children: tmp } // tmp 里是指向一个 tmp1 的对象
tmp = [] // tmp2
pusher = { value: "item2" } // pusher2
arr.push(puhser) // 意味着往 tmp1 里添加
arr = tmp // arr 将持有 tmp2 的引用
// 最终
s = { value: "item0", children: tmp } // tmp包含pusher1,pusher1的children的tmp包含pusher2
arr = tmp = [pusher2] // 最终 tmp2 === tmp1
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.