一、面向過程:全部的工做都是現寫現用。javascript
二、面向對象:是一種編程思想,許多功能事先已經編寫好了,在使用時,只須要關注功能的運用,而不須要這個功能的具體實現過程。java
將相關的變量和函數組合成一個總體,這個總體叫作對象,對象中的變量叫作屬性,變量中的函數叫作方法。javascript中的對象相似字典。程序員
<script type="text/javascript"> var Tom = { name : 'tom', age : 18, showname : function(){ alert('個人名字叫'+this.name); }, showage : function(){ alert('我今年'+this.age+'歲'); } } </script>
<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>
<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>
<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>
<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>