How to use dynamic function to get any type of variable in javascript

Votes : 5
604
You can use several methods like instanceof , typeof or constructor, Object.prototype.toString to get object or variable name in javascript.
object එකක හරි variable එකක හරි නමක් ගන්න instanceof , typeof , constructor, Object.prototype.toString වැනි ක්‍රම කීපයක් මේ සදහා ඔබට භාවිත කලහැකිය.
var arr = [1,2,3];
var obj_arr = {'one':1,'color':'red','five':5};
var fun = function (){    console.log('test ...');  }

function abc(){	console.log('test ...'); }
var obj_fun  =  new  abc();

var obj = {
    key:1256,
    abc : function (){    console.log('test ...');  }
}
INSTANCEOF
Using the instanceof operator can get type of object. but it has few own potential issues.
මුලින්ම instanceof භාවිතා කරල පුලුවන් object type එක ගන්න. නමුත් instanceof වලටම ආවෙනික ගැටලු කීපයක් තිබෙනනවා.
The instanceof operator is shootable for testing object types
මේ ක්‍රමය ගොඩක් වේලාවට object types සදහා භාවිත කරන්න පුලුවන්.
(arr instanceof Array); // true
(arr instanceof Object); // true
 
(obj_arr instanceof Array); // false
(obj_arr  instanceof Object); // true

(obj_fun  instanceof Object); // true
(obj_fun  instanceof Array); // false
But fail for working with primitive values
String, Boolean වගේ primitive values වලට හරියටම වැඩ කරන්නේ නැ.
'abc' instanceof String // false
true instanceof Boolean // false

3 instanceof Number // false
parseInt(25)  instanceof Number // false
You can convert integer or float values into number, then instanceof work for it.
integer or float අගයන් number අගයන් බවට පරිවර්තනය ( convert ) කලොත්, එයට instanceof වැඩ කරනවා.
new Number(3) instanceof Number // true
.CONSTRUCTOR
.constructor better than instanceof becouse .constructor work with primitive values too.
instanceof එකේ තිබෙන ගැටලුව .constructor වල වෙන්නෑ. primitive අගයන් සදහාද ක්‍රියා කරනවා.
3..constructor === Number // true
'abc'.constructor === String // true
true.constructor === Boolean // true
document.constructor === HTMLDocument // true
window.constructor === Window // true
document.getElementById.constructor  === Function // true

Math.constructor === Math // false
Math.constructor === Object // true
typeof
typeof method can be used to get object type as a string value.
typeof method එක object එකේ type එක string අගයක් ලෙස ලබාගන්න.
typeof  3; // number 
typeof  'abc'; // string
typeof  true; // boolean 
typeof arr; // object  
typeof  obj_arr; // object
typeof obj_fun; // object
typeof document; // object
typeof window; // object
typeof document; // object
typeof Math;// object
typeof method is suitable for primitive values but not for object type variables. This method useless becuase return 'object' as a string for all object type of variables.
typeof method එක හොඳයි primitive අගයන් සදහා නමුත් object type variables සඳහා object ලෙස string අගයක්, එකම අගයක් ලබාදීම නිසා වැඩි ප්‍රයෝජනයක් ගනන් බෑ.
Object.prototype.toString
Object.prototype.toString can be used to get object type as string value by using object's prototype converting to string.
Object.prototype.toString භාවිත කරලා Object එකක prototype එක toString කරලා ලේසියෙන්ම object type string අගයක් ලෙස ලබාගත හැක.
Object.prototype.toString.call('string');  // [object String]
Object.prototype.toString.call(125); // [object Number]
Object.prototype.toString.call(true); // [object Boolean]
Object.prototype.toString.call(window); // [object Window]
Object.prototype.toString.call(document); // [object HTMLDocument]
Object.prototype.toString.call(document.getElementById); // [object Function]
Object.prototype.toString.call(Math); // [object Math] 
Object.prototype.toString.call(arr); // [object Array]
Object.prototype.toString.call(obj_arr); // [object Object]
Object.prototype.toString.call(fun); // [object Function]
Object.prototype.toString.call(obj_fun); // [object Object]
Object.prototype.toString.call(obj); // [object Object]
Object.prototype.toString.call(null); // [object Null] 
Object.prototype.toString.call(undefined);  // [object Undefined] 
var toString = Object.prototype.toString;

toString.call(new Date);    // [object Date]
toString.call(new String);  // [object String] 
FINAL METHOD
function ESObjType(obj){
    /*
     * Get object type by string
     * @param (any object value) obj -  object value 
     * 
     * @return (string) object type
     * 
     * source : http://codrate.com/articles/dynamically-generate-slug-in-php
     */
    return Object.prototype.toString.call(obj).slice(8, -1);
}
ESObjType('string');  // String
ESObjType(125); // Number
ESObjType(true); // Boolean
ESObjType(null); // Null 
ESObjType(undefined);  // Undefined
ESObjType(document); // HTMLDocument 
ESObjType(obj_arr); // Object
ESObjType(obj_arr); // Object
ESObjType(fun); // Function 
asked 4 months,13 days ago

What is the codrate ?

codrate.com is a standard, fast cross browsing and highly versatile site. It is useful for many large number of Program Development Industries. So you can get support form Codrators , who are the codrate's joiners around world to help your program developments, You can answer other codrator's questions. Communicate with them. Share your knowledge with them. Do you have an interest in programming, So publish your articles about programming. It will help to maintain your professional co-profile. Actually codrate.com is not such as a regular web site. It will be gave new experience, best narrow cross-browser view, reduce processing time to receive browsing request, it's mean do not wasting your time to browsing codrate's web pages because it has been upgrade always modern coding ways. So, what do you waiting for ?. Try your own.

Copyright 2015 Pride - Company. Design by Esila