标签:一个 day index 函数 console ++ object 参数 value
<html>
<body>
<p id="p"> this is ppp </p>
<p id="p2"> this is ppp22 </p>
<p id="p3"> this is ppp33 </p>
<script>
//arguments
//arguments是每一个函数内部的一个对象
//可以访问实际传递给函数的参数的信息。
/*
function add(a,b){
//console.log(add.length);//形参的个数
//console.log(arguments.length);//实际传过来的参数
var total = 0;
for(var i = 0;i< arguments.length;i++){
total += arguments[i];
}
return total;
}
var result = add(1,2,3,4,5);
var result2 = add(1,2);
console.log(result);
console.log(result2);
function setStyle(){
if(arguments.length < 1
|| arguments.length > 2){
return ;
}
var pele = document.getElementById("p");
if(arguments.length == 1){
return pele.style[arguments[0]] ;
}else{
pele.style[arguments[0]] = arguments[1];
//pele.style.backgroundColor = "red"
}
}
//setStyle("fontSize","18px");
setStyle("backgroundColor","red");
var styleValue = setStyle("backgroundColor");
alert(styleValue);
setStyle("fontSize","18px");
var styleValue2 = setStyle("fontSize");
alert(styleValue2);
//声明的时候参数的个数与实际调用时无关
//foreach循环
var arr = [100,300,800];
for(var data in arr){
//alert(arr[data]); //循环数组时,data是下标
}
var o ={name:"cj",age:22};
for(var p in o){
//alert(p);
//alert(o[p]);
}
function myForEach(eles,callback){
for(var i=0;i< eles.length;i++){
callback(i,eles[i]);
}
}
var alls = document.getElementsByTagName("p");
myForEach(alls,function(index,ele){
//alert(index);
//alert(ele);
ele.style.backgroundColor = "red";
});*/
//重载(个数不同,类型不同)
/*
function prop(){
var firstP = document.getElementById("p");
if(arguments.length == 1){
var temp = arguments[0];
for(p in temp) {
firstP.style[p] = temp[p];
}
}else if(arguments.length == 2) {
firstP.style[arguments[0]] = arguments[1];
}
}
//prop("backgroundColor","black");
//prop("fontSize","38px");
prop({
backgroundColor:"red",
fontSize:"38px"
});
*/
/*
*/
//回调函数
//js里面,狭隘的理解,参数作为参数,并在函数
//实现体里面被调用
/*
function a(){
b(); //c();d();
}
function a(callback){
callback();
}
function b(){
}
function c(){
}
a(b);
a(c);
a(function(){});
function CjOneDay(maiyan){
console.log("chouyan");
var yan = maiyan(10);
console.log(yan);
}
function banZhangMaiYan(money){
if(money == 20){
return "wuyeshen";
}else{
return "zhongnanhai";
}
}
CjOneDay(banZhangMaiYan);
//
function myForEachBgColor(eles){
for(var i=0;i< eles.length;i++){
if(i % 2 == 0 ) {
eles[i].style.backgroundColor = "red";
}else{
eles[i].style.backgroundColor = "green";
}
}
}
function myForEachFontSize(eles){
for(var i=0;i< eles.length;i++){
if(i % 2 == 0 ) {
eles[i].style.fontSize = "30px";
}else{
eles[i].style.fontSize = "10px";
}
}
}
function myForEach(eles,callback){
for(var i=0;i< eles.length;i++){
callback(i,eles[i]);
}
}
var allp = document.getElementsByTagName("p");
//myForEachBgColor(allp);
//myForEachFontSize(allp);
myForEach(allp,function(index,ele){
if(index % 2 == 0 ) {
ele.style.fontSize = "30px";
}else{
ele.style.fontSize = "10px";
}
});
myForEach(allp,function(index,ele){
if(index % 2 == 0 ) {
ele.style.backgroundColor = "yellow";
}else{
ele.style.backgroundColor = "green";
}
});
*/
//对象继承
var child ={name:"banzhang"};
var parent = {age:18,gender:true}
function myExtend(cObject,pObject) {
for(var p in pObject){
cObject[p] = pObject[p];
}
return cObject;
}
var exChild = myExtend(child,parent);
//alert(exChild);
console.log(exChild);
var exChild = superExtend(child,parent,p2,p3,p4);
</script>
</body>
</html>
arguments,foreach循环,重载,回调函数,对象继承
标签:一个 day index 函数 console ++ object 参数 value
原文地址:http://www.cnblogs.com/luowenjun-kio/p/7899952.html