Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mixin in js

function mixin<T extends { new (...args: any[]): {} }>(base: T, obj) {
  return class extends base {
    constructor(...args: any[]) {
      super(...args);
      Object.assign(this, obj);
    }
  };
}

export default mixin;
Comment

JS mixin implementation

/**
 * @module Core
 * @namespace window
 */
/**
 * Static class for mixing in functionality into objects.
 * @class mixin
 * @static
 */
(function(window, Object)
{
	// The mixin function already exists
	if ("mixin" in window) return;
 
	/**
	 * Mixin functionality to an object
	 *
	 * @example
		mixin(instance, MyClass);
	 *
	 * @constructor
	 * @method mixin
	 * @param {*} target The instance object to add functionality to
	 * @param {function|String} superClass The parent reference or full classname
	 * @param {*} [args] Any additional arguments to pass to the constructor of the superClass
	 * @return {*} Return reference to target
	 */
	var mixin = function(target, superClass)
	{
		if (DEBUG && !superClass)
		{
			throw 'Did not supply a valid mixin class';
		}
 
		// Include using string
		if (typeof superClass === "string")
		{
			superClass = window.include(superClass);
		}
 
		// Check for existence of prototype
		if (!superClass.prototype)
		{
			if (DEBUG)
			{
				throw 'The mixin class does not have a valid protoype';
			}
			else
			{
				throw 'no mixin prototype';
			}
		}
		//loop over mixin prototype to add functions
		var p = superClass.prototype;
 
		for (var prop in p)
		{
			// For things that we set using Object.defineProperty
			// very important that enumerable:true for the 
			// defineProperty options
			var propDesc = Object.getOwnPropertyDescriptor(p, prop);
			if (propDesc)
			{
				Object.defineProperty(target, prop, propDesc);
			}
			else
			{
				// Should cover all other prototype methods/properties
				target[prop] = p[prop];
			}
		}
		// call mixin on target and apply any arguments
		superClass.apply(target, Array.prototype.slice.call(arguments, 2));
		return target;
	};
 
	// Assign to the window namespace
	window.mixin = mixin;
 
}(window, Object));
Comment

mixin in js

shantay
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert todays date to json datetime 
Javascript :: create a friend component react js 
Javascript :: complite nodejs remove ubuntu 
Javascript :: creating theme.json fullsiteediting 
Javascript :: array loop 
Javascript :: fetch Mongodb find() results with Backbone 
Javascript :: Ghost-Blog Maria DB Issue 
Javascript :: how to use file js 
Javascript :: Save Function To Different Name 
Javascript :: Angular active router change event 
Javascript :: react using props and parent state 
Javascript :: video link on videojs 
Javascript :: nodejs express parse query params boolean 
Javascript :: Automatic update javascript fileversion 
Javascript :: JS time set 24H so AM PM tag 
Javascript :: use stigviewr 
Javascript :: Simplest Template Example 
Javascript :: Use a stack to convert the infix expression x*y-2 into post-fix 
Javascript :: what is prototype-based in javascreipt 
Javascript :: upload file to s3 using pre signed url javascript 
Javascript :: how to make react host on https localhost 
Javascript :: modalInstance.result.then when execute 
Javascript :: puppeteer click is not working 
Javascript :: change placeholder color in material ui 
Javascript :: javascript detect if active element is writable 
Javascript :: html select structure 
Javascript :: javascript complex literal 2 
Javascript :: javascript dom functions 
Javascript :: how to set maxLength of input type number in react 
Javascript :: filter by last week 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =