plasusu / study-notes Goto Github PK
View Code? Open in Web Editor NEWa project for recording study notes.
a project for recording study notes.
元字符 | 意义 | 备注 |
---|---|---|
. | 除换行符以外的任意字符 | 内容 |
\w | 字母或下划线或数字或汉字 | 内容 |
\s | 任意的空白符(空格、制表符、换行符...) | 内容 |
\d | 数字 | 内容 |
\b | 单词的开始或结尾 | 位置 |
^ | 字符串开始 | 位置 |
$ | 字符串结束 | 位置 |
注意点:
1. 不要把.
当成连接符!!!
2. \b
一般用来匹配单词
3. 其他元字符: (
、)
代码 | 意义 |
---|---|
* | 0次及以上 |
+ | 1次及以上 |
? | 0或1次 |
{n} | 重复n次 |
{n,} | n次及以上 |
{n,m} | n-m次(包含n,m) |
? 等价于 {0, 1}
问题:重复n次或者m次如何表示?
[abc]
匹配a、b、c中的任意一个[.?]
匹配.
或者 ?
中的任意一个,这里的.
和?
不再是元字符类似于js中的||
\(?0\d{2}[)-]?\d{8}
本意想去匹配021-12345678或者(021)12345678这两种形式,但是按照上面正则(02112345678、02112345678、021)12345678...乱七八糟的形式都会被匹配到。
分支条件登场:
\(0\d{2}\d{8}|0\d{2}-\d{8}
上面的问题: 重复n次或者m次如何表示? 答案也有了,利用分支条件。
() 可以进行分组,比如:
(\d{1,3}.){3}\d{1,3}
匹配到类似1.2.300.23
代码 | 意义 |
---|---|
\W | 除字母、数字、下划线、汉字以为的字符 |
\S | 除空白符外的字符 |
\D | 非数字 |
\B | 不是单词开头或结尾的位置 |
[^x] | 除了x以外的任意字符 |
[^asd] | 除了asd以外的任意字符 |
Vue源码刚开始看,src/core/index.js
下有这样一段代码:
import { isServerRendering } from 'core/util/env'
import { FunctionalRenderContext } from 'core/vdom/create-functional-component'
...
Object.defineProperty(Vue.prototype, '$isServer', {
get: isServerRendering
})
...
Object.defineProperty(Vue, 'FunctionalRenderContext', {
value: FunctionalRenderContext
})
...
产生一个疑惑,为什么要用Object.defineProperty
?不可以直接给属性赋值吗?比如:
Vue.prototype.$isServer = isServerRendering()
emmm..其实对Object.defineProperty
的理解并不深,平时写业务代码也基本没用到过,只有在这种源码库看到过,话不多说,打开MDN看呗。
首先,有四个属性:
共有属性 | 数据描述符 | 存取描述符 |
---|---|---|
configurable enumerable |
value writable |
get set |
数据描述符和存取描述符 只能二选一, 像最上面代码中$isServer
用到了存取描述符,FunctionalRenderContext
则用的是数据描述符,两者如果同时存在会报错。
const a = {}
// Uncaught TypeError: Invalid property descriptor. Cannot both specify accessors and a value or writable attribute
Object.defineProperty(a, 'x', {
get() {return 1},
value: 2,
})
需要关注的是这些属性的默认值:
configurable
: falseenumerable
: falsevalue
: undefinedwritable
: falseget
: undefinedset
: undefined几个属性的意义其他都好记,configurable
和 writable
之前就没搞清楚过,这次细看之后,理解了。看个demo:
const obj1 = {}
Object.defineProperty(obj1, 'x', {
value: 2,
configurable: true,
writable: false
})
obj1.x = 3
console.log(obj1.x) // 2 这个例子就是writeable的作用,控制是否能改变x的值,注意是x的值
const obj2 = {}
Object.defineProperty(obj2, 'x', {
value: 2,
configurable: false,
writable: true
})
obj2.x = 3
console.log(obj2.x) // 3 可见configurable并不能控制x的值能否改变,往下看它的真实作用
const obj3 = {}
Object.defineProperty(obj3, 'x', {
value: 2,
configurable: false,
writable: true
})
// Uncaught SyntaxError: Identifier 'obj3' has already been declared
Object.defineProperty(obj3, 'x', {
configurable: true
})
// configurable其实去控制表中6个属性中除去两个数据描述符属性意外的4个属性 是否能被修改, 以及x这个属性可否被删除
回过头再来看Vue源码中的定义方式
Object.defineProperty(Vue.prototype, '$isServer', {
get: isServerRendering
})
// 等价于
Object.defineProperty(Vue.prototype, '$isServer', {
configurable: false,
enumerable: false,
get: isServerRendering
set: undefined
})
Object.defineProperty(Vue, 'FunctionalRenderContext', {
value: FunctionalRenderContext
})
// 等价于
Object.defineProperty(Vue, 'FunctionalRenderContext', {
configurable: false,
enumerable: false,
value: FunctionalRenderContext,
writable: false
})
...
附上这些默认值后就很明了了,源码给Vue原型和Vue挂载的两个属性都是只读属性。作者不希望这两个值被框架使用者在使用过程中修改,所以给这两个值赋予了只读属性。如果以对象的方式赋值,那么这些属性的默认值是true。
平时在写业务代码中可能用不到这样的方式,但是在设计一些框架或者库的时候,可以借鉴学习这种写法。
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.