Professional Documents
Culture Documents
5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
You have 2 free stories left this month. Sign up and get an extra one for free.
bit sh Follow
May 15 · 5 min read
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 1/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 2/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 3/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
obj.toString() // 111
_toString.call(obj) // [object Object]
/hello/.toString() // /hello/
_toString.call(/hello/) // [object RegExp]
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 4/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 5/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
From EcmaScript
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 6/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
toRawType(null) // "Null"
toRawType(/sdfsd/) //"RegExp"
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 7/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
With the above function, we can get the type of a JavaScript variable.
vuejs/vue
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework
for building UI on the web. - vuejs/vue
github.com
(line 62)
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 8/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
function computed(str) {
// Suppose the calculation in the funtion is very time consuming
console.log('2000s have passed')
return 'a result'
}
We can write a cached function to wrap around our target function. This
cache function takes the target function as an argument and returns a new
wrapped function. Inside the cached function, we can cache the result of
the previous function call with an Object or Map .
1 function cached(fn){
2 // Create an object to store the results returned after each function execution.
3 const cache = Object.create(null);
4
5 // Returns the wrapped function
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 9/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
Here is an example:
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 10/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
vuejs/vue
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework
for building UI on the web. - vuejs/vue
github.com
(line 153)
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 11/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
camelize('hello-world')
// "helloWorld"
vuejs/vue
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework
for building UI on the web. - vuejs/vue
github.com
(line 164)
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 12/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
Today, with the rapid development of the front end, our JavaScript code
may be executed in different runtime environments. In order to better adapt
to the various runtime environments, we need to determine which runtime
environment the current code is executing in. Let’s take a look at how Vue
determines the running environment:
vuejs/vue
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework
for building UI on the web. - vuejs/vue
github.com
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 13/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
(line 6)
function fn(){}
fn.toString() // "function fn(){}"
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 14/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
vuejs/vue
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework
for building UI on the web. - vuejs/vue
github.com
(line 58)
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 15/16
5/24/2020 5 JavaScript Tips I Learned From Vue Source Code - Level Up Coding
https://levelup.gitconnected.com/5-javascript-tips-i-learned-from-vue-source-code-6095df4e9bc1 16/16