Giter Site home page Giter Site logo

web's People

web's Issues

snippet

  • 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 

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.