<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>html