Skip to content

Instantly share code, notes, and snippets.

@naveenrawat51
Last active June 21, 2019 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naveenrawat51/5be03df3b4cd4565ee8084fbd84462df to your computer and use it in GitHub Desktop.
Save naveenrawat51/5be03df3b4cd4565ee8084fbd84462df to your computer and use it in GitHub Desktop.
call(), apply() and bind() Implementation in Javascript, Implementation of Native JavaScript Methods call(), apply() and bind() --- naveenrawat51@gmail.com
+++++++++++++++++ call() Implementation +++++++++++++++++++++
Function.prototype.myCall = function(obj1){
let random = Math.random();
obj1[random] = this;
let args = [];
for( let i =1; i<arguments.length; i++ ){
args.push(arguments[i])
}
return obj1[random](...args)
}
let obj = {
name: 'Naveen',
age: 28
}
function fn1(company, practice){
console.log(`${this.name} works in ${company} in ${practice} and his age is ${this.age}`)
}
fn1.myCall(obj, 'GSPANN', 'UI Development');
+++++++++++++++++ apply() Implementation +++++++++++++++++++++
Function.prototype.myApply = function(obj1){
let random = Math.random();
obj1[random] = this;
let args = [];
for( let i =1; i<arguments[1].length; i++ ){
args.push(...arguments[i])
}
return obj1[random](...args)
}
let obj = {
name: 'Naveen',
age: 28
}
function fn1(company, practice){
console.log(`${this.name} works in ${company} in ${practice} and his age is ${this.age}`)
}
const arr1 = ['GSPANN', 'UI Development'];
fn1.myApply(obj, arr1);
+++++++++++++++++ bind() Implementation +++++++++++++++++++++
Function.prototype.myOwnBind = function(newThis) {
if (typeof this !== "function") {
throw new Error(this + "cannot be bound as it's not callable");
}
return function() {
return this.apply(newThis);
};
};
let obj = {
name: 'Naveen',
age: 28,
fn1(company, practice){
console.log(`${this.name} works in ${company} in ${practice} and his age is ${this.age}`)
}
}
let obj1 = obj.fn1.bind(obj, 'GSPANN', 'UI Development');
obj1();
___________________________________- Multi level array to flat array ______________________________________________
let arr = [1,2,3,[4,5,[6,7,[8,9,[10,11], [12,13]]]], 14,15,[16,17,[18,[19,[20]]]]];
function flatArray(array){
let result = [];
for(let i = 0; i < array.length; i++){
if(array[i].constructor === Array){
result.push(...flatArray(array[i]))
}else {
result.push(array[i])
}
}
return result;
}
console.log(flatArray(arr));
___________________________________- Multi level object to flat object ______________________________________________
let obj1 = {
a: 'b',
c: {
d: 'e',
f: {
g: 'h',
i: {
j: 'k',
x: {
z: 'p',
r: {
x:'a'
}
}
}
},
l: 'm',
n : {
o : {
p: 'q',
r: {
s: 't'
}
}
}
}
}
let flattenObj = function(objects) {
let flattenedObj = {};
for (let obj in objects) {
if (objects[obj].constructor === Object) {
let flatObj = flattenObj(objects[obj]);
for (let i in flatObj) {
flattenedObj[i] = flatObj[i];
}
} else {
flattenedObj[obj] = objects[obj];
}
}
return flattenedObj;
}
console.log(flattenObj(obj1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment