JSFiddle - React, Tailwind, and code Playground
by deepak sisodiya
JavaScript
// Question 1
function MyArray() {
var b = Array.apply(this , arguments);
b.__proto__ = MyArray.prototype;
return b;
}
MyArray.prototype = Object.create(Array.prototype);
MyArray.prototype.add = function(value) {
this.push(value);
};
MyArray.prototype.addAll = function() {
if(typeof arguments[0] == "object") {
var x;
for(x in arguments[0]) {
this.add(arguments[0][x]);
}
}else {
for(var i=0; i < arguments.length; i++) {
this.add(arguments[i]);
}
}
};
var collection = new MyArray(1, 2, 3, 4);
alert(collection instanceof Array); //true
alert(collection instanceof MyArray); //true
alert(collection); //[1,2,3,4];
alert(collection.length); //4
collection.add("hello");
alert(collection[4]); //"hello"
alert(collection.length); //5
collection.push("world");
alert(collection.length); //6
alert(collection[5]); //world
collection.addAll(["java", "script"]);
alert(collection.length); //8
alert(collection); //[1,2,3,4,"hello", "world", "java", "script"]
collection[8] = "last";
alert(collection.length); //9
alert(collection.pop()); //"last"
alert(collection.length); //8
alert(Array.prototype.addAll); //undefined
var alert = function (str) {
var st = document.createTextNode(str);
var p = document.createElement('p');
p.appendChild(st);
document.querySelector('body').appendChild(p);
}