码迷,mamicode.com
首页 > Web开发 > 详细

4.js中的函数与方法

时间:2018-01-20 14:04:06      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:func   概念   his   bsp   function   log   避免   square   http   

4.js中的函数(相当于java中的方法)
1.在js中函数也是一种值,可以被赋给变量的。
//函数plus1
function plus1(x) {
return x+1;
};
 
//调用函数
plus1(3); //输出结果4
 
var square = function (x) {
return x*x;
};
 
//用变量装载函数,相当于把函数放入一个容器中
square(plus1(3)); //输出结果16
 
2.js中被称之为方法的是需要满足:方法是属于对象的(js中对象是种数据类型),这一条件(this关键字的使用);
var a = {
name: 4,
sex:7
};
 
a.speak = function () {
return this.name + this.sex;//使用this对象,避免自己调自己
};
 
a.speak();
 
 

4.js中的函数与方法

标签:func   概念   his   bsp   function   log   避免   square   http   

原文地址:https://www.cnblogs.com/Nick-Hu/p/8320221.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!