面向?qū)ο?/p>
面向過程與面向?qū)ο缶幊?/p>
1、面向過程:所有的工作都是現(xiàn)寫現(xiàn)用珊泳。
2鲁冯、面向?qū)ο螅菏且环N編程思想,許多功能事先已經(jīng)編寫好了色查,在使用時(shí)薯演,只需要關(guān)注功能的運(yùn)用,而不需要這個(gè)功能的具體實(shí)現(xiàn)過程秧了。
javascript對(duì)象
將相關(guān)的變量和函數(shù)組合成一個(gè)整體跨扮,這個(gè)整體叫做對(duì)象,對(duì)象中的變量叫做屬性验毡,變量中的函數(shù)叫做方法衡创。javascript中的對(duì)象類似字典。
創(chuàng)建對(duì)象的方法
1晶通、單體
<script type="text/javascript">
var Tom = {
? ? name : 'tom',
? ? age : 18,
? ? showname : function(){
? ? ? ? alert('我的名字叫'+this.name);? ?
? ? },
? ? showage : function(){
? ? ? ? alert('我今年'+this.age+'歲');? ?
? ? }
}
</script>
2璃氢、工廠模式
<script type="text/javascript">
function Person(name,age,job){
? ? var o = new Object();
? ? o.name = name;
? ? o.age = age;
? ? o.job = job;
? ? o.showname = function(){
? ? ? ? alert('我的名字叫'+this.name);? ?
? ? };
? ? o.showage = function(){
? ? ? ? alert('我今年'+this.age+'歲');? ?
? ? };
? ? o.showjob = function(){
? ? ? ? alert('我的工作是'+this.job);? ?
? ? };
? ? return o;
}
var tom = Person('tom',18,'程序員');
tom.showname();
</script>
2、構(gòu)造函數(shù)
<script type="text/javascript">
? ? function Person(name,age,job){? ? ? ? ? ?
? ? ? ? this.name = name;
? ? ? ? this.age = age;
? ? ? ? this.job = job;
? ? ? ? this.showname = function(){
? ? ? ? ? ? alert('我的名字叫'+this.name);? ?
? ? ? ? };
? ? ? ? this.showage = function(){
? ? ? ? ? ? alert('我今年'+this.age+'歲');? ?
? ? ? ? };
? ? ? ? this.showjob = function(){
? ? ? ? ? ? alert('我的工作是'+this.job);? ?
? ? ? ? };
? ? }
? ? var tom = new Person('tom',18,'程序員');
? ? var jack = new Person('jack',19,'銷售');
? ? alert(tom.showjob==jack.showjob);
</script>
3狮辽、原型模式
<script type="text/javascript">
? ? function Person(name,age,job){? ? ? ?
? ? ? ? this.name = name;
? ? ? ? this.age = age;
? ? ? ? this.job = job;
? ? }
? ? Person.prototype.showname = function(){
? ? ? ? alert('我的名字叫'+this.name);? ?
? ? };
? ? Person.prototype.showage = function(){
? ? ? ? alert('我今年'+this.age+'歲');? ?
? ? };
? ? Person.prototype.showjob = function(){
? ? ? ? alert('我的工作是'+this.job);? ?
? ? };
? ? var tom = new Person('tom',18,'程序員');
? ? var jack = new Person('jack',19,'銷售');
? ? alert(tom.showjob==jack.showjob);
</script>
4一也、繼承
<script type="text/javascript">
? ? ? ? function fclass(name,age){
? ? ? ? ? ? this.name = name;
? ? ? ? ? ? this.age = age;
? ? ? ? }
? ? ? ? fclass.prototype.showname = function(){
? ? ? ? ? ? alert(this.name);
? ? ? ? }
? ? ? ? fclass.prototype.showage = function(){
? ? ? ? ? ? alert(this.age);
? ? ? ? }
? ? ? ? function sclass(name,age,job)
? ? ? ? {
? ? ? ? ? ? fclass.call(this,name,age);
? ? ? ? ? ? this.job = job;
? ? ? ? }
? ? ? ? sclass.prototype = new fclass();
? ? ? ? sclass.prototype.showjob = function(){
? ? ? ? ? ? alert(this.job);
? ? ? ? }
? ? ? ? var tom = new sclass('tom',19,'全棧工程師');
? ? ? ? tom.showname();
? ? ? ? tom.showage();
? ? ? ? tom.showjob();
? ? </script>
作者:飄零_0f71
鏈接:http://www.reibang.com/p/148252b9aa8c
來源:簡書
簡書著作權(quán)歸作者所有巢寡,任何形式的轉(zhuǎn)載都請(qǐng)聯(lián)系作者獲得授權(quán)并注明出處。