js中bind方法是什么

匿名用户 分类:前端面试题

bind方法返回一个被绑定的新函数。

我们可以指定特定的this值或“owner”对象,以便我们稍后在代码中使用它。

调用apply方法会立即调用该函数,而不是像bind方法那样返回一个新函数。

import React from 'react';

class MyComponent extends React.Component {
     constructor(props){
          super(props); 
          this.state = {
             value : ""
          }  
          this.handleChange = this.handleChange.bind(this); 
          // Binds the "handleChange" method to the "MyComponent" component
     }

     handleChange(e){
       //do something amazing here
     }

     render(){
        return (
              <>
                <input type={this.props.type}
                        value={this.state.value}
                     onChange={this.handleChange}                      
                  />
              </>
        )
     }
}

一线大厂高级前端编写,前端初中阶面试题,帮助初学者应聘,需要联系微信:javadudu

回复

我来回复
  • 暂无回复内容