Posted On April 10, 2019

Common JavaScript Techniques

kimconnect 0 comments
blog.KimConnect.com >> Codes >> Common JavaScript Techniques
Common JavaScript Techniques:

1. Demonstate your understanding of they keyword typeof
console.log(typeof typeof 1);

// this follows the order of operations from keyword closest to the value. In this case, (typeof 1) shall return a string called "number". Next, (typeof "number") would return a string valued as "string"

2. Accessing private methods
var someObject = {
name: 'Some Object',
getName: function(){
return this.name;
}
};
var newObject = someObject.getName.bind(someObject);

// In order to access a function with a return of "this" value (meaning someObject), someObject must be specified as the argument to the .bind function. This is required even though the someObject has already been instantiated with the call to someObject.getName property.

3. Array operations
var testArray = [1,2,3,4,5];
// Add items toward the end
testArray.push(6);
testArray = [7,...testArray];

// Insert items at the beginning
testArray.shift(0);
testArray = [...testArray,-1];

// splice at certain positions
testArray = [-2,...testArray,8];

4. Inheritance in JavaScript
// Constructor
function animal(type){
this.member = type;
}
// Add public methods
animal.prototype.name = function(string){
return this.member +"name: "+string;
}
// Invoke the new public method
var myPet = new animal('dog');
myPet.name("Dragon"); // returns "dog name: Dragon"

5. How to use private
...By observing Closures as characteristics of JavaScript, where inner functions have access to the outer variables and parameters. Here are some examples:

function publicFunction(){
this.name = value;
}
publicFunction.prototype.name = "some name";

function privateFunction(){
var that = this;
var name = value;
function name(){};
}

function priviledgedFunction(){
this.name = function(){};
}

6. Demonstrate currying
var operate = function(x){
if (x=='add'){
return function(y,z) {
var result = y + z;
return result;
};
};//endif add
if (x=='subtract'){
return function(y,z) {
var result = y - z;
return result;
};
};//endif add
};

var addNums = operate(add);
var subtractNums = operate(subtract);
addNums(5,5); //10
subtractNums(5,5); //0

7. Self-executing anonymous function
;(function(){
// some code;
})()

// this function has no name, and it's automatically invoked upon instantiation

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Front End Web Development Menu 2019

Deployment Register a domain name: Google Domains, GodaddyManaged or Shared hosting: AWS, Hostgator, InmotionFTP/SFTP: Filezilla,…

How To Modify a Windows Service

Here's the freebie code: # Version 0.02 $serviceName='Windows_Exporter' $newExePath=$false $newSwitches=" --log.format logger:eventlog?name=$serviceName --collectors.enabled os,cpu,cs,logical_disk,net,tcp,service,textfile" function…

PowerShell: Windows Automated Disk Cleanup

##################################################################################     <#      This script is created to automate the cleanup activity. Doing so will benefit to reduce the size of disk.     This script will perform the following   1. Clear windows temp and user temp folder   2. Empty recycle bin   3. Disk Cleanup   4. Clear CBS cabinet log files   5. Clear downloaded patches   6. Clear downloaded driver   7. Clean download folder      Note:  …