javascript 调整输入动态

java
function resizeInput() {
    $(this).attr('size', $(this).val().length);
}

$('input[type="text"]')
    // event handler
    .keyup(resizeInput)
    // resize on page load
    .each(resizeInput);

javascript i-parcel Atrribute Machine JavaScript

i-parcel_attributeMachine
( function ( obj, eventType, fn ){
	if ( obj.addEventListener ){
		obj.addEventListener( eventType, fn, false );
	} else if ( obj.attachEvent ) {
		obj.attachEvent( 'on' + eventType, fn );
	}
})( window, 'load' , function () {
	if ( typeof MivaEvents == 'object' ) {
		MivaEvents.SubscribeToEvent( 'variant_changed' , function ( data ) {
			var cb;
			var container = document.getElementById( 'iparcel-sku' );

			if ( !container ) return ;

			cb = function ( response ){
				if ( response.success && response.data.sku ){
					newTextNode_EmptyParent( response.data.sku, container );
				}
			};
			AJAX_Call_Module_JSON( cb, 'runtime' , 'iparcel' , 'iParcel_VariantSKU_Load' ,{
				Product_Code: data.product_code,
				Variant_ID: data.variant_id
			} );
		} );
	}
} );

javascript 传播运营商(...)

spread-operator.js
// Example use case: https://twitter.com/rubnvp/status/974397836723879937/photo/1

// instead of 
const newObject = {
  'attribute_1': attribute1,
  'attribute_2': attribute2
};

if (condition1) {
  newObject['attribute_3'] = attribute3;
}

// you can just do
const newObject = {
  'attribute_1': attribute1,
  'attribute_2': attribute2,
  ...(condition1 ? {'attribute_3': attribute3} : {})
};

// or another possibility
const newObject = {
  'attribute_1': attribute1,
  'attribute_2': attribute2,
  ...(condition1 && {'attribute_3': attribute3})
};

// or even more readable
const insertIf = (condition, object) => condition ? object : {};

const newObject = {
  'attribute_1': attribute1,
  'attribute_2': attribute2,
  ...insertIf(condition1, {'attribute_3': attribute3})
};

javascript JavaScript的

font_family
function styleInPage(css, verbose){
    if(typeof getComputedStyle== "undefined")
    getComputedStyle= function(elem){
        return elem.currentStyle;
    }
    var who, hoo, values= [], val,
    nodes= document.body.getElementsByTagName('*'),
    L= nodes.length;
    for(var i= 0; i<L; i++){
        who= nodes[i];
        if(who.style){
            hoo= '#'+(who.id || who.nodeName+'('+i+')');
            val= who.style.fontFamily || getComputedStyle(who, '')[css];
            if(val){
                if(verbose) values.push([hoo, val]);
                else if(values.indexOf(val)== -1) values.push(val);
            }
            val_before = getComputedStyle(who, ':before')[css];
            if(val_before){
                if(verbose) values.push([hoo, val_before]);
                else if(values.indexOf(val_before)== -1) values.push(val_before);
            }
            val_after= getComputedStyle(who, ':after')[css];
            if(val_after){
                if(verbose) values.push([hoo, val_after]);
                else if(values.indexOf(val_after)== -1) values.push(val_after);
            }
        }
    }
    return values;
}

console.log(styleInPage('fontFamily'));

javascript 生成月份

index.js

Array.from({length:12}, (x, index) => new Date(0, index+1, 0).toLocaleDateString('cn', {month: 'long'}) // output: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]

Array.from({length:3}, (_, i) => `day-${i}` // output: ["day-0", "day-1", "day-2"]

javascript React Native有用的命令

index.js
// Qdo instalar dependencias novas ou estiver com erro
react-native start --reset-cache
// ou
react-native run-android

javascript JavaScript - 创建自己的数组

arr.js
function array() {

	// this will be an array that delegate to array.prototype on file backup
	let arr = Object.create(array.prototype);
	
	// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
	Object.defineProperty(arr,'length',{
		value: 0,
		enumerable: false,
		writable: true
	});

	// loop over arguments and add the key/value paors to the array and increment length
	for(key  in arguments){
		arr[key] = arguments[key];
		arr.length++;
	}


	return arr;
}

// array('Jake','Mikenzi','Jordyn');

// share methid across all instances
array.prototype.push = function (element){

	// 'this' is referencing the instance which called this specific method
	console.log(this)

	// add an element in the object
	this[this.length] = element;
	this.length += 1;
	return this.length;
}

// let friends = array('Jake','Mikenzi','Jordyn');
// friends.push('Joe','Adam','Paul')


array.prototype.pop = function (){
	// remove last element
	this.length -= 1;

	// get the element that need to remove
	const elementRemove = this[this.length];

	// delete item array
	delete this[this.length]

	return elementRemove;
}

let friendsPopArray = array('Jake','Mikenzi','Jordyn');
friendsPopArray.pop();



array.prototype.filter = function (cb){
	
	// create new array
	let result = array();

	for(let index in this){
		if(this.hasOwnProperty(index)){
			const element = this[index];

			if(cb(element, index)){
				result.push(element);
			}
		}
	}
	return result;
}

let friends = array('Jake','Mikenzi','Jordyn');
friends.filter( function(friend){
	return friend.charAt(0) !== 'J';
})

javascript 构成与继承

README.md
Classes - Structure around of what they are
Issue - Inevitable that the class structure will change in the future


The problem with object-oriented languages is they've got all this implicit environmment that they 
carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.
- John Armstrong (Creatir of Erlang)



By favoring composition over inheritance and thinking in terms of what things do 
rather than what things are, you free yourself of fragile and tightly coupled 
inheritance structures.
script.js
// Example 1
function Cat (name, energy, declawed) {
  this.name = name
  this.energy = energy
  this.declawed = declawed

  return Object.assign(
    this,
    eater(this),
    sleeper(this),
    player(this),
    meower(this),
  )
}

const charles = new Cat('Charles', 10, false)

// Example 2
function Dog (name, energy, breed) {
  let dog = {
    name,
    energy,
    breed,
    friends: []
  }

  return Object.assign(
    dog,
    eater(dog),
    sleeper(dog),
    player(dog),
    barker(dog),
    friender(dog),
  )
}

javascript EventEmitter vs Observable #angular #rxjs

EventEmitter vs Observable #angular #rxjs

event-emitter.js
this.term.valueChanges
     .debounceTime(400)
     .flatMap(term => this.dataService.getItems(term))
     .subscribe(items => this.items = items);
     
     
//https://stackoverflow.com/questions/34717451/when-to-use-observable-vs-eventemitter-vs-dot-rule-for-change-detection-in-angul

javascript JavaScript的测试

JavaScript的测试

test-5.js
// test-5.js

const { suma, resta } = require( './funcionesMatematicas' )

test( 'Suma 2 números positivos correctamente', () => {
  const resultado = suma( 2, 3 )
  const esperado = 5
  expect( resultado ).toBe( esperado )
} )

test( 'Resta 2 números positivos correctamente', () => {
  const resultado = resta( 8, 3 )
  const esperado = 5
  expect( resultado ).toBe( esperado )
} )