JavaScript 函数Function 基础

函数是JS的引用数据类型

匿名函数
function() {
//Code here
}


给一个函数名
function foo() {
//code here
}

或者
var foo = function() {
//Code here
}


执行一个匿名函数
(function() {
//code here
})();

When a function is invoked with fewer arguments than are declared, the additional arguments have the undefined value.

如果函数被调用时,所给的参数不够,缺少的参数将传递undefined

// Append the names of the enumerable properties of object o to the
// array a, and return a. If a is omitted or null, create and return
// a new array
function copyPropertyNamesToArray(o, /* optional */ a) {
if (!a) a = []; // If undefined or null, use a blank array
for(var property in o) a.push(property);
return a;
}


以上这个函数,我们在调用的时候有很灵活的方式

// Get property names of objects o and p
var a = copyPropertyNamesToArray(o); // Get o's properties into a new array
copyPropertyNamesToArray(p,a); // append p's properties to that array


若提供第二参数,则直接在第二个参数的数组中操作


之前那段判断是否为空的语句可以简化成:
a = a || [];
//it returns a if a is defined and non-null, even if a is empty. Otherwise, it returns a new, empty array.


aruguments对象
[quote]Within the body of a function, the identifier arguments has special meaning. arguments is a special property that refers to an object known as the Arguments object.

The Arguments object allows full access to these argument values, even when some or all are unnamed.
Furthermore, like true arrays, arguments has a length property that specifies the number of elements it contains.

the Arguments object defines a callee property that refers to the function that is currently being executed.
This property is rarely useful, but it can be used to allow unnamed functions to invoke themselves recursively. [/quote]

arguments对象包含函数调用时传递的所有参数;
aruguments对象像数组一样,包含length属性
aruguments的callee属性持有对当前函数的引用

function(x) {
if (x <= 1) return 1;
return x * arguments.callee(x-1);
}


Function Properties and Methods
函数的属性和方法

The length Property
[quote]The length property of the Function object specifies exactly how many declared parameters a function has. Note that unlike arguments.length, this length property is available both inside and outside of the function body.[/quote]

length属性可以告诉你这个函数定了多少参数,与arguments.length不同,它在函数内外都有用

The apply() and call() Methods
[quote]These methods allow you to invoke a function as if it were a method of some other object. [/quote]

f.call(o, 1, 2);

和一下代码等效:
o.m = f;
o.m(1,2);
delete o.m;


apply()和call()类似,只是apply()第二个参数是传递给函数的参数数组

Javascript 1.2实现apply()了,但直到Javascript 1.5才实现call()

JSGD里面给出了一些对象的工具函数

// Return an array that holds the names of the enumerable properties of o
//返回包含对象中所有属性名的数组
function getPropertyNames(/* object */o) {
var r = [];
for(name in o) r.push(name);
return r;
}

// Copy the enumerable properties of the object from to the object to.
// If to is null, a new object is created. The function returns to or the
// newly created object.
// 将一个对象的属性复制到另一个对象中
function copyProperties(/* object */ from, /* optional object */ to) {
if (!to) to = {};
for(p in from) to[p] = from[p];
return to;
}

// Copy the enumerable properties of the object from to the object to,
// but only the ones that are not already defined by to.
// This is useful, for example, when from contains default values that
// we want to use if they are not already defined in to.
// 求两个对象的并集,且赋给to对象
function copyUndefinedProperties(/* object */ from, /* object */ to) {
for(p in from) {
if (!p in to) to[p] = from[p];
}
}
实战教程:用YOLOv11+PyQt5打造工地安全帽检测系统(附完整数据集) 本文提供了一份完整的实战指南,详细介绍了如何利用YOLOv11目标检测模型与PyQt5界面库,从零开始构建一个智能工地安全帽检测桌面应用。内容涵盖高质量数据集的准备与标注、YOLOv11模型训练与调优,以及如何使用PyQt5开发流畅、专业的图形用户界面,最终实现一个可实际部署的AI安全监控系统。 阅读详情

相关推荐

最新测算源码PHP算命源码-风水八字-八字起名-塔罗牌占卜

未来,随着技术的发展和用户需求的变化,我们可以继续对系统进行升级和优化,以满足更多用户的需求和期望。数据库设计:创建一个名为bazi的数据库表,包含以下字段:id(自增ID)、name(姓名)、sex(性别)、birth_date(出生日期)、birth_time(出生时间)、bazi(八字结果)。数据库设计:创建一个包含塔罗牌信息的数据库表,如tarot_cards,包含字段:id(塔罗牌ID)、name(塔罗牌名称)、meaning(塔罗牌含义)等。根据操作系统的不同,PHP的安装方法会有所不同。

shanbeizi的博客 2420

学习前端的第二十一天——function函数

function函数的介绍及用法

Robergean的博客 3858

【ESP32-IDF笔记】20-配置以太网网络(W5500)

基于ESP32IDF和官方的ethernet驱动,配置W5500以太网驱动芯片,实现ESP32的有线网络

别看了,就这些信息 3474

java js中 function函数报错_关于JavaScript基础的相关讲解

JavaScript的变量及数据类型问题:关于JavaScript中的变量定义:var 变量名 = 值;注意:1> 在js中,var可以用来定义任何数据类型的变量,可以省略,但不建议省略;2> var可以重复定义变量(后面的值会被覆盖掉),是因为JavaScript弱类型语言,而Java是一种强类型语言;3> 如果一个变量,但是变量没有值(undefined),没有定义的变量不...

weixin_42153793的博客 673

【项目实战-MATLAB】:基于机器学习的虹膜识别系统设计

虹膜识别主要包括虹膜定位、虹膜裁剪、极坐标变换、归一化、HOG特征提取、虹膜识别六个主要步骤。本系统采用MATLAB作为开发工具实现 主要算法:霍夫变换预处理 HOG特征提取 SVM进行识别

qq_45047246的博客 6537

从零开始:手把手教你用VOACAP软件预测短波最佳工作频率(OWF)

本文详细介绍了如何使用VOACAP软件预测短波通信的最佳工作频率(OWF),涵盖软件安装、参数配置、电离层模型设置及结果解读。通过实战案例和优化技巧,帮助用户掌握短波传播特性,提升通信效率。特别适合业余无线电爱好者和专业通信人员参考。

weixin_30897079的博客 649

JS基础:1.9 函数function

wuzhongchao的专栏 324

SAP FICO会计凭证过账接口

-------------------------------------增加职能范围-ZHANGZG-2022.09.07 注释掉-2022.09.20。CONCATENATE '总账' ls_doc_item-hkont '要求输入成本对象值!*-------------------------------------增加职能范围-ZHANGZG-2022.09.07。*-------------------------------------增加职能范围-ZHANGZG-2022.09.07。

dingqing86的专栏 1080

Js函数function基础理解

正文:我们知道,在js中,函数实际上是一个对象,每个函数都是Function类型的实例,并且都与其他引用类型一样具有属性和方法。因此,函数名实际上是指向函数对象的指针,不与某个函数绑定。在常见的两种定义方式(见下文)之外,还有一种定义的方式能更直观的体现出这个概念: var sum = new Function("num1", "num2", "return num1 + num2...

aikenqiu5098的博客 90

改进后的yolov8导出进行部署到rk3588上时,导出为onnx格式报错aten::fft_fft2算子不支持导出...如何解决?

🏆本文收录于《全栈Bug调优(实战版)》专栏,主要记录项目实战过程中所遇到的Bug或因后果及提供真实有效的解决方案,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!! 备注:部分问题/疑难杂症搜集于互联网。

**My Coding Family** 1800

(持续更新中!)详解【计算机类&面试真题】军队文职考试 ——第一期(真题+解析)| TCP、UDP的区别;程序局部性;ICMP协议及主要功能;重定位所需的计算机硬件

在当今信息化时代,军队中对于计算机专业人才的需求与日俱增。因此,了解面试和复试中常见的问题和要求对于准备应聘者来说至关重要。本文将围绕面试的准备要点、常见问题以及面试技巧进行探讨,并提供一些建议和经验。通过了解面试和复试的准备要点、常见问题以及技巧,读者将能够提升应对能力和成功率。本文为准备军队文职面试和计划考研复试的读者提供了有价值的参考和指导。通过阅读本篇博客,读者将能够获得关于军队文职计算机类面试和计算机考研复试的全面了解,提升自身的应对能力和成功率。

追光者♂:记录、分享、总结、提升,现象级专栏《Python从入门到人工智能》作者,无惧黑暗,坚信曙光 1690

JavaScript基础之函数--function

function 函数(1) 首先定义一个对象 var user= { name:"张三", age:18 }; 声明一个函数,打印obj信息 function say(obj){ console.log(obj.name+":"+obj.age); } 一:调用函数say() say(user); 此时控制台打印user信息 二、声明一个新的函数,如下 function fun(a...

weixin_44134810的博客 241

智能运营模型的建设与运用实践

一项运营活动的全流程,主要包含客群挖掘与筛选、产品与权益选择、触点与活动选择、运营策略调优等关键环节,涉及客群管理、活动管理、权益管理、触达管理、智能调控等系统支撑需求,如今工商银行已开展与其相对应的推荐模型、分析模型等的研发与实践。自2023年9月该模型被知客运营平台吸收并上线以来,已累计有远程银行中心、吉林分行、重庆分行等30余家分行或机构使用,赋能融e借、积存金、结售汇、基金、理财、存款等12种场景,产生营销客户清单达千万人次,有力促进了保守型理财、天天盈、添益快线、寿险等产品的销售。

银行信息系统应用架构详解 850

Javascript基础——详解function函数

1.3. 关于function JavaScript的所有代码都是由function组成,function即函数的类型。JavaScript的函数有两种写法:“定义式”和“变量式”。 定义式: function test1(){ alert(‘hello,world’); } 变量式: var test2 = function(){ alert(‘hello,world’); } 我们可以用typeof(test1)和typeof(test2)查看test1和test2的类型都为func

weoln的专栏 967

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

java js中 function函数报错_浅析JS中对函数function的理解(基础篇)

正文:我们知道,在js中,函数实际上是一个对象,每个函数都是Function类型的实例,并且都与其他引用类型一样具有属性和方法。因此,函数名实际上是指向函数对象的指针,不与某个函数绑定。在常见的两种定义方式(见下文)之外,还有一种定义的方式能更直观的体现出这个概念:var sum = new Function("num1", "num2", "return num1 + num2"); //不推荐...

weixin_31837543的博客 378

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

理解JS的函数(Function)基础

基本组成 理解参数 注意 定义函数的两种方式函数声明提升 函数表达式 递归函数中使用argumentscallee基本组成 js函数使用function关键字来声明,后跟一组参数以及函数体。函数在定义时不必指定是否返回值。实际上,任何函数在任何时候都可以通过return语句后跟要返回的值来实现返回值。 return语句可以不带任何返回值,此时函数在停止执行后返回undefined值 理解参

sqliang的专栏 949

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

前端学习笔记——Js函数function基础理解

学习内容来自:《JavaScript高级程序设计(第三版)》 正文:我们知道,在js中,函数实际上是一个对象,每个函数都是Function类型的实例,并且都与其他引用类型一样具有属性和方法。因此,函数名实际上是指向函数对象的指针,不与某个函数绑定。在常见的两种定义方式(见下文)之外,还有一种定义的方式能更直观的体现出这个概念: var sum = new Function("nu...

dishi9269的博客 243

【JavaScript】定义函数的三种方式

在js中,我们一般定义函数有三种方式: 函数声明 函数表达式 构造函数Function 函数声明 常规的方式 将函数挂载到全局的Window上 fn();//函数声明可以先调用,在声明 function fn(参数..){ console.log("这是函数声明") return 返回值 } 函数表达式 将函数当成一个值来处理 const fn = function() { console.log("这是函数表达式"); } fn();//函数表达式必须先声明,再调用 构造函数F

往前爬的蜗牛 1064

JavaScript基础之箭头函数与普通function函数的区别

声明函数方式有多种,但是当前比较关心的是ES5和ES6的俩种类型,也就是function类型的函数和箭头函数: // ES5的函数 function fun () {} // ES6 () => {} 函数的定义就是这俩中方式,今天我们不讲别的,只讲讲他们的区别是什么? (1)箭头函数不能作为构造函数,不能使用new关键字 (2)this的指向不同 ES5函数的this取决于谁调用,那么this就指向谁; ES6箭头函数本身没有this,箭头函数的内部this,指向了其外

lee前端技术站 1278

js function 返回值_JS函数基础与递归原理

四种定义函数匿名函数具名函数赋值函数箭头函数匿名函数function 具名函数function 赋值函数let 箭头函数箭头函数表达式的语法比函数表达式更简洁。更适用于那些本来需要匿名函数的地方,并且它不能用作构造函数。let 变量全局变量顶级作用域里声明的变量http://window.xxxlet 局部变量用{ }包括起来的代码为局部变量,除了使用window.a = 1声明变量,也不能使用v...

weixin_39882623的博客 282

JS基础7-函数(function)

1.函数 函数就是包裹在花括号中的代码块,前面使用了关键词 function function functionname() { 这里是要执行的代码 }关键词 function 必须是小写的,并且必须以与函数名称相同的大小写来调用函数。 <script type="text/javascript"> //函数的声明num1 function...

diajiu9420的博客 160

JS基礎:Function.prototype函數原型的三個方法

JS 基礎:Function.prototype 三大方法 文章目錄JS 基礎:Function.prototype 三大方法簡介參考正文目的`Function.prototype.call` & `Function.prototype.apply`call & apply 語法call & apply Sample`Function.prototype.bind`bind 語法bind Sample應用self 替換bind 函數arrow function 箭頭函數結語 簡介 F

超悠閒的博客 1928
上一篇: JavaScript 对象Object和数组Array
下一篇: JavaScript Prototype基础
QuLong
博客等级 码龄19年 0粉丝 40原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值