Giter Club home page Giter Club logo

blog's People

Contributors

eidonjoe avatar

Stargazers

 avatar

Watchers

 avatar  avatar

blog's Issues

正则表达式之exec用法

w3school链接, JavaScript exec() 方法

需要注意的是, 当exec中执行的正则表达式是全局正则表达式时(正则表达式带/g的?), 在exec方法找到了与表达式匹配的文本时, 会将正则表达式的lastIndex属性(这属性哪来的?怎么能确定这个值?)设置为匹配文本的最后一个字符的下一个位置, 也就是说, 当使用同样的正则表达式再次调用exec方法时, 匹配到的文本将是从上一次匹配结果后面的位置进行匹配的结果, 所以我们可以通过反复调用exec方法来遍历字符串中所有匹配的文本, 当找不到匹配文本时, 该方法将返回null, 并将lastIndex属性重置为0

apply, call, bind知识记录

apply, call, bind均为函数内置方法, 存在与函数prototype上.
apply, call, 用于在特定函数作用域上调用函数, 实际上相当于改变函数体内部的this指向,

apply

apply函数接收两个参数第一个是函数需要运行的作用域, 第二个是函数运行所需要的参数, 可以是参数数组, 也可以是arguments对象.

function sayHello(name, age) {
    console.log('hello ' + name, age + ' years old');
}
function apply1(name, age) {
    return sayHello.apply(this, arguments);
}
function apply2(name, age) {
    return sayHello.apply(this,[name, age]);
}
apply1('roger1', 18); // hello roger1 18 years old
apply2('roger2', 19); // hello roger2 19 years old

call

call 方法与apply方法用法相同, 只是传递函数参数时必须一一列举出来.

function sayHello(name, age) {
    console.log('hello ' + name, age + ' years old');
}
function call1(name, age) {
    return sayHello.call(this, ...arguments);
}
function call2(name, age) {
    return sayHello.call(this,name, age);
}
call1('roger1', 18); // hello roger1 18 years old
call2('roger2', 19); // hello roger2 19 years old

所以在不给函数传参数的情况下, 两种方法是一致的.

改变函数this指向

function Person() {
    this.name = 'Tom';
}
Person.prototype.sayAge = function(age) {
    console.log(this.name + ' is ' + age);
}
let person = new Person();
person.sayAge(18); // Tom is 18
function NewPerson() {
    this.name = 'Bob';
}
let newPerson = new NewPerson();
person.sayAge.call(newPerson, 19); // Bob is 19

bind

bind函数, 会创建一个新的函数实例, 新创建函数的this值会被绑定到传给bind函数中的值

function Person() {
    this.name = 'Tom';
}
Person.prototype.sayAge = function(age) {
    console.log(this.name + ' is ' + age);
}
let person = new Person();
function NewPerson() {
    this.name = 'Bob';
}
let newPerson = new NewPerson();
let newSayAge = person.sayAge.bind(newPerson);
newSayAge(19); // Bob is 19

在事件绑定中使用bind函数来保证this始终可以访问到

在使用事件监听函数(或setTimeout, setInterval)传入处理事件的方法时, this指向window, 所以访问不到函数运行时正确的环境, 解决方案之一(还可以使用es6的箭头函数)就是可以使用bind进行函数运行环境的绑定.不过被绑定的函数会比普通函数拥有更多的内存开销, 而且绑定过的函数是多重函数调用, 也会比普通函数慢一些, 所以最好在需要的时候使用

使用bind创建偏函数

偏函数即可以使某些函数拥有默认的参数, 只需要在使用bind时将默认的参数在this后面传递就可以了
当被绑定的函数调用时, 这些参数会被插入到目标函数参数列表的前面, 而函数传入的参数则在这些参数的后面

function list() {
  return Array.prototype.slice.call(arguments);
}

function addArguments(arg1, arg2) {
    return arg1 + arg2
}

var list1 = list(1, 2, 3); // [1, 2, 3]

var result1 = addArguments(1, 2); // 3

// 创建一个函数,它拥有预设参数列表。
var leadingThirtysevenList = list.bind(null, 37);

// 创建一个函数,它拥有预设的第一个参数
var addThirtySeven = addArguments.bind(null, 37); 

var list2 = leadingThirtysevenList(); 
// [37]

var list3 = leadingThirtysevenList(1, 2, 3); 
// [37, 1, 2, 3]

var result2 = addThirtySeven(5); 
// 37 + 5 = 42 

var result3 = addThirtySeven(5, 10);
// 37 + 5 = 42 ,第二个参数被忽略

React.cloneElement

官方文档

React.cloneElement

React.cloneElement(
  element,
  [props],
  [...children]
)

等同于

<element.type {...element.props} {...props}>{children}</element.type>

以 element 元素为样板克隆并返回新的 React 元素。返回元素的 props 是将新的 props 与原始元素的 props 浅层合并后的结果。新的子元素将取代现有的子元素,而来自原始元素的 key 和 ref 将被保留。

该做法会保留了组件的 ref。这意味着当通过 ref 获取子节点时,你将不会意外地从你祖先节点上窃取它。相同的 ref 将添加到克隆后的新元素中。

  • 该方法可以用于向组件的children中添加属性, 即element为传入的children

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.