数组排序
var arr = [1,6,3,5,7,2]; 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!",25)); console.log(ref.length); ref = function(){ return arguments[0]; } console.log(ref("hi!")); console.log(ref("hi!",25)); console.log(ref.length);
函数sum计算数据和
function sum(){ var result = 0; for(var i=0;i<arguments.length;i++){ result+= arguments[i]; } return result; } console.log("sum() - "+sum());
// 类重载
function showa(){ if(arguments.length === 0){ console.log("没有参数"); }else if(arguments.length === 1){ console.log("一个参数:"+arguments[0]); } } 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);
时间: 2024-12-12 21:23:50