شما اینجا هستید

به طور کلی، یک تابع زیر برنامه ای است که به وسیله یک کد خارجی (یا در موارد بازگشتی داخلی) فراخوانی میگردد.

مانند برنامه، یک تابع از دنباله ای از دستورات که بدنه تابع نام دارند تشکیل شده است. مقادیر را میتوان به تابع داد و تابع هم میتواند یک مقدار را برگرداند.

تعریف تابع

تعریف تابع ( همچنین اعلان تابع هم نام دارد) شامل واژگان کلیدی زیر است:

  • نام تابع

  • آرگومانهای داده شده به تابع که درون پرانتز قرار دارند و با کاما از هم جدا شده اند.

  • دستورات جاوا اسکریپت که در تابع درون براکت {} نوشته میشوند.

برای مثال، کد زیر یک تابع ساده به نام square را تعریف میکند:

function square(number) {
  return number * number;
}

تابع square یک آرگومان به نام number به عنوان ورودی میگیرد. تابع از یک دستور که مقدار بازگشتی تابع که حاصلضرب مقدار متغیر number در خودش است را برمیگرداند تشکیل شده است. دستور return مقدار بازگشتی توسط تابع را مشخص میکند.

پارامترهای اولیه (مانند number) با مقدار به تابع داده میشود؛ مقدار در داخل تابع استفاده میشود، اما اگر تابع مقدار پارامتر را تغییر دهد، این تغییر به طور سراسر منعکس نمیگردد.

اگر یک شی (مانند آرایه یا شی) را به عنوان پارامتر به تابع بدهیم و تابع ویژگی های شی را تغییر بدهد، تغییرات در خارج از تابع مانند مثال زیر قابل مشاهده خواهند بود:

function myFunc(theObject) {
  theObject.make = "Toyota";
}

var mycar = {make: "Honda", model: "Accord", year: 1998};
var x, y;

x = mycar.make; // x gets the value "Honda"

myFunc(mycar);
y = mycar.make; // y gets the value "Toyota"
                // (the make property was changed by the function)

عبارت تابع (استفاده از تابع مانند یک عبارت)

همینطور که تابع اعلان شده در بالا از لحاظ دستوری مانند یک دستور (بیانیه) تعریف شده بود، توابع میتوانند توسط عبارت تابع نیز ایجاد بگردند. مانند توابع ناشناس؛ آن نباید نام داشته باشد. برای مثال، تابع square میتواند به شکل زیر تعریف گردد.

function myFunc(theObject) {
  theObject.make = "Toyota";
}

var mycar = {make: "Honda", model: "Accord", year: 1998};
var x, y;

x = mycar.make; // x gets the value "Honda"

myFunc(mycar);
y = mycar.make; // y gets the value "Toyota"
                // (the make property was changed by the function)

با این حال، میتوان یک نام برای عبارت تابع ارایه داد تا درون تابع برای ارجاع دادن به خودش، یا در debugger (ابزار اشکال زدایی) برای شناسایی تابع در پشته ردیابی استفاده بگردد.

var factorial = function fac(n) { return n<2 ? 1 : n*fac(n-1) };

console.log(factorial(3));

عبارات تابع هنگامی که یک تابع را به عنوان آرگومان به تابع دیگری میدهیم مناسب هستند.مثال زیر تعریف تابع map و سپس فراخوانی یک تابع ناشناس به عنوان اولین پارامتر رانشان میدهد:

function map(f,a) {
  var result = [], // Create a new Array
      i;
  for (i = 0; i != a.length; i++)
    result[i] = f(a[i]);
  return result;
}
map(function(x) {return x * x * x}, [0, 1, 2, 5, 10]);

در جاوا اسکریپت، یک تابع میتواند بر اساس یک شرط تعریف بشود. برای مثال، تعریف تابع زیر، myFunc را فقط اگر num برابر با صفر شود تعریف (مقداردهی) میکند:
var myFunc;
if (num == 0){
  myFunc = function(theObject) {
    theObject.make = "Toyota"
  }
}

 

دیدگاه‌ها

Cialis Viagra Contre http://buycialisuss.com - Buy Cialis 30 Day Cialis 5mg Buy Cialis Acheter Priligy Thaр“р‡lande

how much are viagra pills viagra prank gay friend xhamster female viagra feeling like a young buck on viagra sex commercial husband with viagra too much

viagra def teva's generic viagra buy viagra bull viagra when will viagra be available without a prescription

https://pharm-usa-official.com - over the counter viagra

https://realstlnews.com/locked-out/#comment-62707
http://www.ipc.org.co/agenciadeprensa/index.php/2010/06/04/conflicto-arm...
https://cocosolis.com/ro/easter-eggs-natural-dyes/?unapproved=131355&mod...
http://www.baijiamama.com/space-uid-58184.html
http://hjfbbs.com/space-uid-184621.html

quick loans review quick approval personal loans quick loan quick loans rates quick safe loans

quick 200 dollar loans quick safe loans [url=http://www.aradius.ru/ ]quick loans[/url] quick loans online cash loans quick

faxless quick payday loans quick and easy loans online quick loan quick & easy personal loans get quick loans in usa

quick online payday loans no credit check quick personal loans [url=http://hdd-remont.ru/ ]quick loan[/url] quick pay payday loans new york state quick payday loans

quick loans near me safe quick loans quick loans quick online business loans quick funding loans freedom card

online quick approval installment loans quick online payday loans [url=https://paydailoanz.com/# ] quick loan[/url] safe quick loans quick loans for bad credit

https://paydailoanz.com/# - quick loans

50000 quick loans safe quick loans quick loan quick cash loans huntsville al quick loans near me

quick loans under 500 quick loans albany ga [url=https://paydailoanz.com/# ] quick loan[/url] fast quick loans quick loans dayton blvd

https://paydailoanz.com/# - quick loans

quick payday loans today quick loans for millenials quick loan best egg loans/quick quick payday loans today

quick cash loans reviews quick and loans [url=https://paydailoanz.com/# ] quick loan[/url] ways to get quick loans does quick and loans have wifi

https://paydailoanz.com/# - quick loan

bad credit quick loans personal loans quick quick loans quick payday loans wichita ks quick online loans

quick loans albany ga quick installment loans online [url=https://paydailoanz.com/# ] quick loans[/url] private lenders funding quick loans no collateral no up front fees no vredit checks quick loans bad credit with monthly payments

https://paydailoanz.com/# - quick loan

Antibiotics Similar To Amoxicillin [url=http://cialibuy.com/#]Cialis[/url] Getting Viagra Over The Counter Cialis How Is Amoxil Used

make money blogging make money now instant payout work from home legitimate ways make extra money apps to make money

quick ways to make money how to make fast money [url=https://make-money-blogs.com/# ] work from home[/url] make money now instant payout how to make money with a website

https://make-money-blogs.com/# - make money

how to make money on youtube how to make money make money how do youtubers make money ways to make money on the side

how to make money selling drugs how to make money selling drugs [url=https://make-money-blogs.com/# ] make money[/url] diy crafts that make money easy ways to make money

https://make-money-blogs.com/# - work from home

how does venmo make money how to make money on youtube videos make money how to make money fast homemade items that make money

how to make money on the side how to make money online [url=https://make-money-blogs.com/# ] work from home[/url] ways to make extra money how to make money online fast

https://make-money-blogs.com/# - make money

make money at home how to make quick money make money how to make money on instagram how does facebook make money

how to make money on amazon make extra money [url=https://make-money-blogs.com/# ] make money[/url] how to make money online from home how much money do youtubers make

https://make-money-blogs.com/# - make money

make real money online easy ways to make money make money hobbies that make money how to make money selling drugs

photos imgclick easily make money sharing will friends social free file upload how did jerry jones make his money [url=https://make-money-blogs.com/# ] work from home[/url] legitimate ways make extra money how to make money online from home

https://make-money-blogs.com/# - make money

how to make money make money work from home how much money do youtubers make make extra money

how does craigslist make money ways to make extra money [url=https://make-money-blogs.com/# ] make money[/url] free ways to make money online make money on facebook

https://make-money-blogs.com/# - work from home

how to make easy money how to make money as a teen work from home how to make money at home how much money can i make on social security

photos imgclick easily make money sharing will friends social free file upload how to make money on the internet [url=http://u-dachnik.ru/] make money[/url] ways to make money make money at home

https://medafanov.ru/ - make money

how to make money on the side how to make money online for free work from home how to make money with a website how to make extra money from home

how to make money fast how much money can you make on disability [url=http://newsamara.ru/] make money[/url] how did george soros make his money how much money does pewdiepie make

http://acqua-di.ru/ - make money

make money fast legit ways to make money online make money how to make money on twitch how to make money online from home

make money online how to make fast money online [url=http://baniotdelka.ru/] work from home[/url] how do youtubers make money make money from home

http://do-mo.ru/ - work from home

make money now instant payout how does facebook make money make money how does facebook make money make money apps

make money online from home make real money online [url=http://do-mo.ru/] make money[/url] how do youtubers make money how to make money fast

http://holyofholys.ru/ - work from home

how much money does pewdiepie make how to make money in gta 5 work from home how to make money with bitcoin how to make money from home

how to make fast money online how to make money at home [url=http://korolferm.ru/] make money[/url] ways to make money online free ways to make money online

http://na-svet.ru/ - work from home

ways to make money ways to make money at home make money how to make money on instagram how to make money with bitcoin

easy way to make money easy ways to make money [url=http://alekseygerman.ru/] make money[/url] how did tom steyer make his money make extra money from home

http://mobirolo.ru/ - make money

how to make money with bitcoin ways to make money at home make money how to make money on instagram photos imgclick easily make money sharing will friends social free file upload

how to make money on facebook make money online free [url=http://tailandobzor.ru/] make money[/url] how to make money on amazon how to start a blog and make money

http://netorz.ru/ - work from home

how did jeffrey epstein make his money how do you make money on youtube make money how to make fast money online how does craigslist make money

make extra money from home how to make money online how to earn money online or gift cards to amazon [url=http://zakon-2011.ru/] work from home[/url] how to make money without a job how to make money with a website

http://p-stroinn.ru/ - make money

cbd vape [url=https://cbdoilwalmart.com/ ]hemp cbd [/url] cbd drops cbd gummies hemp cbd

cbd vape cbd vape cbd oil cbd oil [url=https://cbdoilwalmart.com/ ]cbd vape [/url]

real money casino http://onlinecasinogamespcx.com/ slots for real money online gambling online casinos

no deposit casino [url=http://onlinecasinogamespcx.com/ ]play online casino [/url] play casino vegas casino slots

world class casino slots play slots online online casino gambling slots online

online casino bonus online casino casino bonus codes casino online

play online casino casino slots play slots online free casino games online [url=http://onlinecasinogamespcx.com/ ]online casino games [/url]

http://onlinecasinogamespcx.com/ slots online online slot games slots for real money casino online slots

slots online online casino gambling online casinos slot games

free casino http://onlinecasinogamespcx.com/ best online casino cashman casino slots casino blackjack

http://onlinecasinogamespcx.com/ online casinos big fish casino world class casino slots slot games

slots online http://onlinecasinosw.com/ casino real money slots games casino play

casino online slots [url=http://onlinecasinosw.com/ ]cashman casino slots [/url] online casino games slots for real money

play casino http://onlinecasinogamespcx.com/ best online casino slots games free free casino games online

slots free play slots real money casino casino online slots [url=http://onlinecasinosw.com/ ]play slots [/url]

online casino online casino free slots play casino [url=http://onlinecasinopc.com/ ]vegas slots online [/url]

http://onlinecasinopc.com/ gold fish casino slots casino blackjack online slots slots games free

casino games http://onlinecasinosw.com/ vegas casino slots slots games free play slots

personal loans with bad credit great lakes loans student loans consolidation loans bad credit loans guaranteed approval

great lakes loans car loans for bad credit [url=https://paydailoanz.com/# ] lawrence o’donnell trump loans[/url] betsy devos forgives loans sallie mae student loans

https://paydailoanz.com/# - discover student loans

https://projecteve.com/emotions-beat-reason-in-b2b/?unapproved=910321&mo...
https://blog.ensci.com/innovationbydesign/2010/10/15/archigram/?unapprov...
http://www.9wanli.net/home.php?mod=space&uid=999336
http://study.xrkky.com/home.php?mod=space&uid=442065
http://bbs.xnnews.com.cn/home.php?mod=space&uid=15492840

صفحه‌ها

افزودن دیدگاه جدید

دیدگاه

  • تگ‌های HTML مجاز: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • خطوط و پاراگراف‌ها بطور خودکار اعمال می‌شوند.
  • نشانی صفحه‌ها وب و پست الکترونیک بصورت خودکار به پیوند تبدیل می‌شوند.

Plain text

  • تگ‌های HTML مجاز نیستند.
  • نشانی صفحه‌ها وب و پست الکترونیک بصورت خودکار به پیوند تبدیل می‌شوند.
  • خطوط و پاراگراف‌ها بطور خودکار اعمال می‌شوند.
كد امنيتي
این پرسش برای جلوگیری از ارسال اسپمهای اتوماتیک است.