js精要之函数
数组排序
var arr = [,,,,,];
arr.sort(function(a,b){
return a-b;
});
console.log(arr);
arguments 参数存储对象
function ref(value){
return value;
}
console.log(ref("hi!"));
console.log(ref("hi!",));
console.log(ref.length);
ref = function(){
return arguments[];
}
console.log(ref("hi!"));
console.log(ref("hi!",));
console.log(ref.length);
函数sum计算数据和
function sum(){
var result = ;
for(var i=;i<arguments.length;i++){
result+= arguments[i];
}
return result;
}
console.log("sum() - "+sum());
// 类重载
function showa(){
if(arguments.length === ){
console.log("没有参数");
}else if(arguments.length === ){
console.log("一个参数:"+arguments[]);
}
}
showa();
字面形式对象调用公共方法
function sayNameForAll(){
console.log(this.name)
}
var person1 = {
name:"zs1",
sayName:sayNameForAll
}
var person2 = {
name:"ls2",
sayName:sayNameForAll
}
var name = "ww3";
person1.sayName();
person2.sayName();
sayNameForAll()
// call();参数1指定this的值,参数2 传给函数的参数
function sayNameForAlla(label){
console.log(label+":"+this.name)
}
var person1a = {
name:"zs1"
}
var person2a = {
name:"ls2"
}
var name = "ccc"
sayNameForAlla.call(this,"name")
sayNameForAlla.call(person1a,"person1a")
// apply()
sayNameForAlla.apply(this,["namea"])
sayNameForAlla.call(person1a,["person1a"])
// 判断对象中是否含有属性 in (包括原型属性)
console.log("name" in person2a); //true
console.log("toString" in person2a); //true
// 判断对象中是否含有属性 hasOwnProperty() (不包括 原型属性) toString()是所有对象都具有的原型属性
console.log(person2a.hasOwnProperty("name"));
console.log(person2a.hasOwnProperty("toString"));
// false
// delete 删除属性
delete person1a.name;
console.log("name" in person1a);
js精要之函数的更多相关文章
- 【Mocha.js 101】钩子函数
前情提要 在上一篇文章<[Mocha.js 101]同步.异步与 Promise>中,我们学会了如何对同步方法.异步回调方法以及 Promise 进行测试. 在本篇文章中,我们将了解到 M ...
- JS魔法堂:函数重载 之 获取变量的数据类型
Brief 有时我们需要根据入参的数据类型来决定调用哪个函数实现,就是说所谓的函数重载(function overloading).因为JS没有内置函数重载的特性,正好给机会我们思考和实现一套这样的机 ...
- js中的回调函数的理解和使用方法
js中的回调函数的理解和使用方法 一. 回调函数的作用 js代码会至上而下一条线执行下去,但是有时候我们需要等到一个操作结束之后再进行下一个操作,这时候就需要用到回调函数. 二. 回调函数的解释 因为 ...
- js两种定义函数、继承方式及区别
一:js两种定义函数的方式及区别 1:函数声明: function sayA() { alert("i am A"); } 2:函数表达式: var sayB = function ...
- WebView中Js与Android本地函数的相互调用
介绍 随着Html5的普及,html在表现力上不一定比原生应用差,并且有很强的扩展兼容性,所以越来越多的应用是采用Html与Android原生混合开发模式实现. 既然要实现混合开发,那么Js与Andr ...
- ASP.NET前台JS与后台CS函数如何互相调用
摘要: 在实际的Web开发中,我们可能会常常遇到后台调用前台JS代码或者前台JS调用后台代码的情况.今天就把比较实用的前后台相互调用的方法总结出来和大家分享. 在实际的Web开发中,我们可能会常常遇到 ...
- JS封装cookie操作函数实例(设置、读取、删除)
本文实例讲述了JS封装cookie操作函数.分享给大家供大家参考,具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...
- js调用父框架函数
if (window.parent && window.parent.frames["frame_main"]) { alert(window.parent.fra ...
- underscore.js中的节流函数debounce及trottle
函数节流 throttle and debounce的相关总结及想法 一开始函数节流的使用场景是:放止一个按钮多次点击多次触发一个功能函数,所以做了一个clearTimeout setTimeou ...
随机推荐
- astah* professional 6.9.0
下载地址:http://members.change-vision.com/files/astah_professional/6_9_0 破解方法:按照Astah Professional 6.9后打 ...
- C# 开发系列(二)
1. 参考文档:http://www.yiibai.com/csharp/csharp_environment_setup.html 2. C# ,ASP.NET HTTP Authorization ...
- html5新增标签集锦
<keygen></keygen><meter low="69" high="80" max="100" op ...
- mysql优化------2 查看系统性能(表大小,I/o性能)
三:判断mysql I/0 性能的一种方式(网络搜集供参考) show global status like 'innodb_dblwr%'\G 如果innodb_dblwr_pages_writ ...
- FZU 1063 三维扫描
水题.DFS求连通块. #include<cstdio> #include<cstring> #include<cmath> #include <iomani ...
- 火狐上的一个post提交工具(主要用于测试接口时候)
添加的过程 安装完后,就可以在下图上,看到一个poster 点击poster就可以看到下图 图中红线圈好的,是必须要填写的 Url是访问路径 Name是参数名称 Value是参数值 需要注意一点的是: ...
- Mysql导入zabbix的sql语句时报错:ERROR 1045 (28000)
#Warning: Using a password on the command line interface can be insecure.#ERROR 1045 (28000): Access ...
- java实现——008旋转数组的最小数字
public class T008 { public static void main(String[] args) { int[] num = { 3, 4, 5, 1, 2 }; System.o ...
- ios framework 开发实战 之 参考
WWDC2014之iOS使用动态库 iOS开发——创建你自己的Framework 使用CocoaPods开发并打包静态库 iOS Framework 和CocoaPods TDD的iOS开发初步以及K ...
- 一段神奇的代码-关于PHP字符变量奇怪现象的解释
首先神奇的PHP是支持字符数据类型的,有同学就写了这样一段代码: for ($c = 'a'; $c <= 'z'; $c++) { echo $c . ' '; } 然而结果却不是他想要的a到 ...