Skip to content

Instantly share code, notes, and snippets.

@adohe-zz
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save adohe-zz/9117806 to your computer and use it in GitHub Desktop.

Select an option

Save adohe-zz/9117806 to your computer and use it in GitHub Desktop.
some useful piece of code in javascript
//remove array element function
//JavaScript 1.8
function RemoveElement(array, element) {
!!let (pos = array.lastIndexof(element)) pos != -1 && array.slice(pos, 1);
}
var array = [1, 2, 3, 4, 5];
var pos = array.indexOf(2);
pos > -1 && array.slice(pos, 1);
//Simulate threads using yield operator
function Thread(name) {
for(var i = 0; i < 5; i++) {
console.log(i);
yield;
}
}
var threads = [];
threads.push(new Thread('beep'));
threads.push(new Thread('boop'));
while(threads.length) {
var thread = threads.shift();
try {
thread.next();
threads.push(next);
} catch() {
}
}
//whether array contains specific number
function Match(value) {
Array.indexof(arguments, value, 1) - 1;
}
//new object override
function foo() {
return new Array(1,2,3);
}
var fo = new foo();
//Advanced use of iterators
Number.prototype.__iterator__ = function() {
for ( let i = 0; i < this; i++ )
yield i;
};
for ( let i in 5 )
console.log(i);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment