面向?qū)ο?/h2>

內(nèi)置對(duì)象
1、document
document.referrer //獲取上一個(gè)跳轉(zhuǎn)頁(yè)面的地址(需要服務(wù)器環(huán)境)

2、location
window.location.href //獲取或者重定url地址
window.location.search //獲取地址參數(shù)部分
window.location.hash //獲取頁(yè)面錨點(diǎn)或者叫哈希值

3、Math
Math.random 獲取0-1的隨機(jī)數(shù)
Math.floor 向下取整
Math.ceil 向上取整

面向?qū)ο?br> 面向過(guò)程與面向?qū)ο缶幊?/p>

1、面向過(guò)程:所有的工作都是現(xiàn)寫(xiě)現(xiàn)用。

2、面向?qū)ο螅菏且环N編程思想,許多功能事先已經(jīng)編寫(xiě)好了,在使用時(shí),只需要關(guān)注功能的運(yùn)用,而不需要這個(gè)功能的具體實(shí)現(xiàn)過(guò)程。

javascript對(duì)象
將相關(guān)的變量和函數(shù)組合成一個(gè)整體,這個(gè)整體叫做對(duì)象,對(duì)象中的變量叫做屬性,變量中的函數(shù)叫做方法。javascript中的對(duì)象類(lèi)似字典。

創(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>

3、構(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,'銷(xiāo)售');
alert(tom.showjob==jack.showjob);
</script>

4、原型模式

  <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,'銷(xiāo)售');
  alert(tom.showjob==jack.showjob);
</script>

5、繼承

  <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>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容