js---17繼承中方法屬性的重寫


function F(){};
var f = new F();
f.name = "cf";
f.hasOwnProperty("name");//true
F.prototype.age = 22;
f.hasOwnProperty("age");//false,age是原型對象的屬性,name是F對象的屬性,不是同一個。hasOwnProperty是對象的方法
F.prototype.isPrototypeOf(f);//true

//多態:編譯時多態,運行時多態:方法重載、重寫
//js不支持同名的方法
var o = {
run:function(){},
run:function(){},  //js同名會覆蓋,run指向的是這個函數對象的地址,地址名加小括號就是這個類對象開始執行。function F(){} 既是類也是對象,new F()說明是一個類,F()函數名加小括號就是函數執行,說明是一個對象。
}

=======================================================================

function demo (a,b) {
    console.log(demo.length);//形參個數,2
    console.log(arguments.length);//實參個數,3
    console.log(arguments[0]);
    console.log(arguments[1]);
}
demo(4,5,6);

function add(){
    var total = 0;
    for (var i = arguments.length - 1; i >= 0; i--) {
        total += arguments[i];
    };
    return total;
}
console.log(add(1));
console.log(add(1,2));//可變長度


function fontSize(){
    var ele = document.getElementById("js");
    if (arguments.length == 0){
        return ele.style.fontSize;
    }else{
        ele.style.fontSize = arguments[0];

    }
}
fontSize(18);
console.log(fontSize());

function setting(){
     var ele = document.getElementById("js");
     if (typeof arguments[0] ==="object"){
         for(p in arguments[0]){//p是key,arguments[p]是value,
            ele.style[p] = arguments[0][p];
        }
    }else{
    ele.style.fontSize = arguments[0];
    ele.style.backgroundColor= arguments[1]
;    }
}
setting(18,"red");
setting({fontSize:20,backgroundColor:"green"});//js里面不能寫同名的方法,所以只能夠對方法的參數做判斷,


==========================================================================


function demo(o){//demo是一個類,o是類的對象屬性
     o.run();//調用屬性的方法
}
var o = {run:function(){
    console.log("o is running...");
}};
demo(o);//類調用,java里面類不可以調用,這是跟java不一樣的。


var p ={run:function(){
    console.log("p is running...");
}};
demo(p);//函數是一個類也是一個對象,函數調用對象就會執行起來。

function F(){};
var f = new F();
F.prototype.run = function(){console.log("111");}//原型區域,對象. 可以訪問
f.run();//111
f.run = function(){console.log("222");};//只是給f自己加了一個方法,沒有改變類的原型對象,相當於方法的重寫。f.什么都是給自己對象加的
f.run();//222
F.prototype.run();//111
f.run = function(){//run指向一個對象的地址
    console.log("222");
    F.prototype.run();//重寫父的,並且還要調用父的,
};
f.run();//222 , 111

=======================================================================


function Parent(){
    this.run = function(){//現在把Parent當成類看,run是一個對象的地址,
        console.log("parent is running...");
    }
}

function Child(){
     Parent.call(this);//繼承父的方法,相當於父有了一個run方法,this.run = function(){console.log("parent is running...");},但是2個方法不是同一個,只是相當於把父的屬性和方法在這里寫了一遍。
     var parentRun = this.run;//用this,parentRun指向run函數的地址,
     this.run = function (){//run重新指向,重寫,添加同名的子類方法
         console.log("child is running...");
         parentRun();//地址名小括號就是對象的執行
     }
}

var c = new Child();//Child看成是類,c是對象
c.run();//run是函數的地址,地址小括號就是對象執行


========================================================================
function Parent(){
    this.name = "333";//只能通過 對象.name 訪問
    var age = 34;//給嵌套函數使用
}
var p = new Parent();

console.log(p.name);//333
console.log(Parent.name);//Parent
console.log(p.age);//undefined, 
console.log(Parent.age);//undefined, 

Parent.aa = "aa"; //靜態屬性,對象. 訪問不到,類. 訪問得到
Parent.prototype.aaa = "asa";//原型公有區域,對象. 訪問得到,類. 訪問不到

console.log(p.aa);//undefined,
console.log(Parent.aa);//aa
console.log(p.aaa);//asa,
console.log(Parent.aaa);//undefined

p.zz = "zz";//只是加給了對象自己,沒有加給類和類的原型
console.log(p.zz);//zz
console.log(Parent.zz);//undefined
console.log(Parent.prototype.zz)//undefin
==========================================================================

function Parent(){

}
Parent.prototype.run = function() {
    console.log("parent");
};

Child.prototype = Object.create(Parent.prototype);//繼承
Child.prototype.constructor = Child;//修正
Child.super  = Parent.prototype; //給Child增加靜態屬性

function Child(){

}

Child.prototype.run=function(){
    console.log('child is running...');
    Child.super.run();
}

var c = new Child();
c.run();

1.類里面的this.屬性給對象用的,靜態屬性、方法給類用,什么都不加的和var給嵌套函數用,什么都不加的在window對象中。(靜態屬性、方法通過F.xx 添加)

2.對象的靜態屬性、方法給自己用。(靜態屬性、方法通過 p.xx 添加)

3.原型里面的屬性、方法是給對象和原型自己用的。(通過 F.prototype.xx 添加)

<html>
<body>
<script type="text/javascript">
function F(){
    this.name = "yw";
    var age = 32;
    sch = 890;
}    

var f = new F();  
alert(f.name);//yw
alert(f.age);//undefined
alert(f.sch);//undefined
F.kr = "gh";
F.ss = function(){alert(123);}
alert(f.kr);//undefined
f.ss();//f.ss is not a function   
alert(F.prototype.kr);//undefined
F.prototype.ss();//F.prototype.ss is not a function



f.a = "a";
f.y = function(){alert("y");}
alert(F.a);//undefined
F.y();//F.y is not a function  
alert(F.prototype.a);//undefined
F.prototype.y();//F.prototype.y is not a function

F.prototype.o = "o";
F.prototype.oo = function(){alert("oo");}
alert(f.o);//o
f.oo();//oo
alert(F.o);//undefined
F.oo();//F.oo is not a function
</script>
</body>

</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM