Function object in JavaScript In Hindi | JS Function object

Image could not load

JS function object

जैसा कि हम जानते हैं कि किसी भी programming language में use होने वाली किसी भी value का एक type होते है , JavaScript में function भी एक value है जिसका भी एक type है।

Well , JavaScript में functions objects होते हैं , जिसका मतलब है कि function में properties को add / remove भी कर सकते हैं।

हालाँकि by default भी हर function की कुछ properties होती हैं।

JS function's name property

किसी भी function की name property , function name return करती हैं।

function test(){ }; console.log(test.name); // test

normal function के अलावा इस property को आप Arrow Function access कर सकते हैं।

let testfun = function(){ }; console.log(testfun.name); // testFun

इसी तरह से किसी function में किसी दुसरे function को as a default value set करने वाले function से भी access कर सकते हैं।

function testfun2(f = function() {}) { console.log(f.name); } testfun2(); // f

इस feature को contextual name कहते हैं , function call करते समय अगर value pass नहीं की तो function name मिलेगा और अगर pass की तो undefined मिलेगा।

for example

testfun2(); // f
testfun2("hi"); // undefined

इसके अलावा किसी Object में define किये गए method में भी name property accessible होती है।

let obj = { fun1() { }, fun2() { }, } console.log(obj.fun1.name); console.log(obj.fun2.name);
fun1
fun2

हालाँकि कुछ cases में name property नहीं भी accessible होती है , जैसे -

let arr = [function(){ }]; console.log(arr[0].name);
<empty string>

जिसका reason है , हम Array में directly function लिख रहे हैं कोई name नहीं दे रहे , इसलिए output में empty string मिल रही है।

JS function's length Property

length property का use करके आप किसी भी function में define किये गए parameter number जान सकते हैं , कि function में कितने parameters define किये गए हैं।

function fun1(x) {} function fun2(x, y) {} function fun3(x, y, ...rest) {} console.log(fun1.length); console.log(fun2.length); console.log(fun3.length);
1
2
3

ध्यान रहे , कि function length property ...rest parameter को count नहीं करती है।

JS function's custom property

जैसा कि आपने अभी पढ़ा कि function में आप अपनी custom properties को add / remove कर सकते हैं , इसके लिए simply property name में कोई value assign कर देनी है -

function counter() { console.log(counter.count++); } counter.count = 1; counter(); // 1 counter(); // 2 counter(); // 3

property कोई variable नहीं है क्योंकि variable को आप directly locally / globally access कर सकते हैं जबकि property को को आप बिना object के साथ access नहीं कर सकते हैं।

I Hope , आपको अब समझ आ गया होगा कि JavaScript में function को as a object किस तरह से use में ले सकते हैं।

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook