javascript - 下面这一段js代码是什么意思呀?
黄舟
黄舟 2017-04-11 11:58:00
[JavaScript讨论组]
function _classCallCheck(instance, Constructor) { 
      if (!(instance instanceof Constructor)) { 
          throw new TypeError("Cannot call a class as a function"); 
       } 
 }

function _possibleConstructorReturn(self, call) { 
    if (!self) {
     throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
      } 
      return call && (typeof call === "object" || typeof call === "function") ? call : self; 
 }

function _inherits(subClass, superClass) { 
      if (typeof superClass !== "function" && superClass !== null) { 
           throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); 
      } 
      subClass.prototype = Object.create(superClass && superClass.prototype, {
       constructor: { value: subClass, enumerable: false, writable: true, configurable: true } 
       });
        if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; 
  }

var HeroHeader = function (_React$Component) {
  _inherits(HeroHeader, _React$Component);

  function HeroHeader() {
    _classCallCheck(this, HeroHeader);

    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

var _this = _possibleConstructorReturn(this, _React$Component.call.apply(_React$Component, [this].concat(args)));

_this.state = { height: undefined };
_this._containerDOM = null;
_this._scrollPosition = 0;
_this.onScroll = _this.onScroll.bind(_this);
return _this;

}

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

全部回复(3)
伊谢尔伦

大体看了下,我看着像是用class XXX extends React.Component{}的类似的东西,里面用了继承等等

PHPz

这这这…

这是React的ES6 Class写法被babel转义到ES5以后生成的一段代码,功能就是实现类的实例化和继承…

话说在哪儿扒的代码…不累么…

迷茫
function _possibleConstructorReturn(self, call) { 
    if (!self) {--判断self 是否存在
     throw new ReferenceError("this hasn't been initialised - super() hasn't been called");---不存在抛出异常
      } 
      ---判断 call的类型,如果call是object或者function,条件为真返回call,要不然就返回self
      return call && (typeof call === "object" || typeof call === "function") ? call : self; 
 }

function _inherits(subClass, superClass) { 
      if (typeof superClass !== "function" && superClass !== null) { --判断subClass的类型必须不等为function且不为空,抛出异常
           throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); 
      } 
      --prototype:原型用法,不用 原型可以去查资料
      --将Object.create,赋予 subClass.prototyp
      subClass.prototype = Object.create(
      ---传入方法中的参数----
      superClass && superClass.prototype, {
       constructor: { value: subClass, enumerable: false, writable: true, configurable: true } 
       }
        ---传入方法中的参数----
       );
       ---判断superClass是否存在
        if (superClass) 
        ---存在值执行下面的方法
        Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;--判断Object.setPrototypeOf,然后值确定执行
  }

var HeroHeader = function (_React$Component) {
    --调用_inherits
  _inherits(HeroHeader, _React$Component);

  function HeroHeader() {
    ---调用_classCallCheck
    _classCallCheck(this, HeroHeader);
    ---遍历赋值
    for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }
大体是这个样子,如果有错误希望能指正!
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号