mirror of
https://github.com/videojs/video.js.git
synced 2025-01-02 06:32:07 +02:00
refactor: remove extend() and tests (#7950)
BREAKING CHANGE: This removes the videojs.extend() method, please use ES6 classes instead.
This commit is contained in:
parent
f2aa0d7d13
commit
1299daf16c
@ -1,78 +0,0 @@
|
||||
/**
|
||||
* @file extend.js
|
||||
* @module extend
|
||||
*/
|
||||
|
||||
import _inherits from '@babel/runtime/helpers/inherits';
|
||||
import log from './utils/log';
|
||||
|
||||
/**
|
||||
* Used to subclass an existing class by emulating ES subclassing using the
|
||||
* `extends` keyword.
|
||||
*
|
||||
* @function
|
||||
* @deprecated
|
||||
* @example
|
||||
* var MyComponent = videojs.extend(videojs.getComponent('Component'), {
|
||||
* myCustomMethod: function() {
|
||||
* // Do things in my method.
|
||||
* }
|
||||
* });
|
||||
*
|
||||
* @param {Function} superClass
|
||||
* The class to inherit from
|
||||
*
|
||||
* @param {Object} [subClassMethods={}]
|
||||
* Methods of the new class
|
||||
*
|
||||
* @return {Function}
|
||||
* The new class with subClassMethods that inherited superClass.
|
||||
*
|
||||
* @deprecated videojs.extend() is deprecated as of v8; use native ES6 classes instead
|
||||
*/
|
||||
const extend = function(superClass, subClassMethods = {}) {
|
||||
log.warn('The extend() method is deprecated. Please use native ES6 classes instead.');
|
||||
|
||||
const isNativeClass = superClass && /^class/.test(superClass.toString());
|
||||
|
||||
let subClass = function() {
|
||||
superClass.apply(this, arguments);
|
||||
};
|
||||
|
||||
// If the provided super class is a native ES6 class,
|
||||
// make the sub class one as well.
|
||||
if (isNativeClass) {
|
||||
subClass = class SubClass extends superClass {};
|
||||
}
|
||||
|
||||
let methods = {};
|
||||
|
||||
if (typeof subClassMethods === 'object') {
|
||||
if (subClassMethods.constructor !== Object.prototype.constructor) {
|
||||
subClass = subClassMethods.constructor;
|
||||
}
|
||||
methods = subClassMethods;
|
||||
} else if (typeof subClassMethods === 'function') {
|
||||
subClass = subClassMethods;
|
||||
}
|
||||
|
||||
if (!isNativeClass) {
|
||||
_inherits(subClass, superClass);
|
||||
}
|
||||
|
||||
// this is needed for backward-compatibility and node compatibility.
|
||||
if (superClass) {
|
||||
subClass.super_ = superClass;
|
||||
}
|
||||
|
||||
// Extend subObj's prototype with functions and other properties from props
|
||||
for (const name in methods) {
|
||||
if (methods.hasOwnProperty(name)) {
|
||||
subClass.prototype[name] = methods[name];
|
||||
}
|
||||
}
|
||||
|
||||
return subClass;
|
||||
};
|
||||
|
||||
export default extend;
|
@ -31,7 +31,6 @@ import * as Dom from './utils/dom.js';
|
||||
import * as browser from './utils/browser.js';
|
||||
import * as Url from './utils/url.js';
|
||||
import * as Obj from './utils/obj';
|
||||
import extend from './extend.js';
|
||||
import xhr from '@videojs/xhr';
|
||||
|
||||
// Include the built-in techs
|
||||
@ -359,15 +358,6 @@ videojs.browser = browser;
|
||||
*/
|
||||
videojs.obj = Obj;
|
||||
|
||||
/**
|
||||
* Deprecated reference to the {@link module:extend~extend|extend function}
|
||||
*
|
||||
* @type {Function}
|
||||
* @see {@link module:extend~extend|extend}
|
||||
* @deprecated Deprecated and will be removed in 9.0. Please use native ES6 classes instead.
|
||||
*/
|
||||
videojs.extend = deprecateForMajor(9, 'videojs.extend', 'native ES6 classes', extend);
|
||||
|
||||
/**
|
||||
* Deprecated reference to the {@link module:obj.merge|merge function}
|
||||
*
|
||||
|
@ -1,55 +0,0 @@
|
||||
/* eslint-env qunit */
|
||||
import extend from '../../src/js/extend.js';
|
||||
|
||||
QUnit.module('extend.js');
|
||||
|
||||
QUnit.test('should add implicit parent constructor call', function(assert) {
|
||||
assert.expect(4);
|
||||
|
||||
let superCalled = false;
|
||||
|
||||
[
|
||||
function() {
|
||||
superCalled = true;
|
||||
},
|
||||
class Parent {
|
||||
constructor() {
|
||||
superCalled = true;
|
||||
}
|
||||
}
|
||||
].forEach(Parent => {
|
||||
const Child = extend(Parent, {
|
||||
foo: 'bar'
|
||||
});
|
||||
const child = new Child();
|
||||
|
||||
assert.ok(superCalled, 'super constructor called');
|
||||
assert.ok(child.foo, 'child properties set');
|
||||
|
||||
superCalled = false;
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.test('should have a super_ pointer', function(assert) {
|
||||
assert.expect(4);
|
||||
|
||||
[function() {}, class Parent {}].forEach(Parent => {
|
||||
const Child = extend(Parent, {
|
||||
foo: 'bar'
|
||||
});
|
||||
const child = new Child();
|
||||
|
||||
assert.ok(child.foo, 'child properties set');
|
||||
assert.equal(child.constructor.super_, Parent, 'super_ is present and equal to the super class');
|
||||
});
|
||||
});
|
||||
|
||||
QUnit.test('sub class is an ES6 class if the super class is', function(assert) {
|
||||
class Parent {}
|
||||
|
||||
const Child = extend(Parent, {
|
||||
foo: 'bar'
|
||||
});
|
||||
|
||||
assert.ok(/^class/.test(Child.toString()), 'sub class is native es6 class');
|
||||
});
|
Loading…
Reference in New Issue
Block a user