Coder Social home page Coder Social logo

study-notes's People

Contributors

plasusu avatar

study-notes's Issues

正则表达式学习笔记(上)

元字符

元字符 意义 备注
. 除换行符以外的任意字符 内容
\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源码系列】Object.defineProperty

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: false
  • enumerable: false
  • value: undefined
  • writable: false
  • get: undefined
  • set: undefined

几个属性的意义其他都好记,configurablewritable之前就没搞清楚过,这次细看之后,理解了。看个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。

平时在写业务代码中可能用不到这样的方式,但是在设计一些框架或者库的时候,可以借鉴学习这种写法。

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.