創(chuàng)建函數(shù)方法的演變

1.函數(shù)聲明

function test1(a){
  a();
}
function test2(){
  console.log("執(zhí)行啦!");
}
test1(test2);//輸出:"執(zhí)行啦!"

2.函數(shù)表達式

var test=function(){
  console.log("執(zhí)行啦!");
}
test();//輸出:"執(zhí)行啦!"

3.工廠模式

function createPerson(name, sex, age) {
  var obj = new Object();
  obj.name = name;
  obj.sex = sex;
  obj.age = age;
  obj.sayName = function () {
    console.log("My name is Tom");
  };
  return obj;
}

var p1 = createPerson('Tom', '男', 20);
var p2 = createPerson('Jack', '男', 21);
console.log(p1.name);//輸出:"Tom"
console.log(p2.name);//輸出:"Jack"

4.構(gòu)造函數(shù)模式

//首字母大寫
function Person(name,sex,age){
  this.name=name;
  this.sex=sex;
  this.age=age;
  this.sayName=function(){
    console.log("My name is Tom");
  }
}
var p1=new Person('Tom','男',20);
var p2=new Person('Jack','男',21);
console.log(p1.constructor===Person);//輸出:true
console.log(p1 instanceof Person);//輸出:true
console.log(p1.sex===p2.sex);//輸出:true
console.log(p1.sayName===p2.sayName);//輸出:false

//函數(shù)優(yōu)化,避免內(nèi)部函數(shù)重復(fù)創(chuàng)建
function Person(name,sex,age){
  this.name=name;
  this.sex=sex;
  this.age=age;
  this.sayName=sayName;
}
function sayName(){
    console.log("My name is Tom");
  }
var p1=new Person('Tom','男',20);
var p2=new Person('Jack','男',21);
console.log(p1.sayName===p2.sayName);//輸出:true
p1.sayName();//輸出:"My name is Tom"

5.原型模式

function Person(){
}
var obj=Person.prototype;
obj.name='Tom';
obj.sex='20';
obj.sayName=function(){
  console.log("My name is Tom");
}
var p1=new Person();
var p2=new Person();
console.log(p1.name);//輸出:"Tom"
console.log(p1.sayName===p2.sayName);//輸出:true
console.log(p1.prototype);//輸出:undefind
//原型對象.constructor=構(gòu)造函數(shù)
//構(gòu)造函數(shù).prototype=原型對象
//實例對象=new 構(gòu)造函數(shù)
//原型對象.isPrototypeOf(實例對象) true

6.組合構(gòu)造函數(shù)模式和原型模式

function Person(name,sex,age){
  this.name=name;
  this.sex=sex;
  this.age=age;
  this.friends=["zhangsan","lisi"];
}
Person.prototype={
  constructor:Person,
  sayName:function(){
    console.log("My name is Tom");
  }
}

var p1=new Person('Tom','男',20);
var p2=new Person('Jack','男',21);
p1.friends.push("wangwu");
console.log(p1.friends);//輸出:["zhangsan","lisi","wangwu"]
console.log(p2.friends);//輸出:["zhangsan","lisi"]
console.log(p1.sayName===p2.sayName);//輸出:true

7.class模式

class  Person{
  constructor(name,sex,age){
    this.name = name;
    this.sex = sex;
    this.age = age;
  }
  sayName(){
    console.log("My name is Tom");
  }
}
var p1=new Person('Tom','男',20);
var p2=new Person('Jack','男',21);
console.log(p1.name);//輸出:"Tom"
console.log(p1.sayName===p2.sayName);//輸出:true
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,525評論 19 139
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,633評論 18 399
  • 本章內(nèi)容 理解對象屬性 理解并創(chuàng)建對象 理解繼承 面向?qū)ο笳Z言有一個標志,那就是它們都有類的概念,而通過類可以創(chuàng)建...
    悶油瓶小張閱讀 963評論 0 1
  • 前言 人生苦多,快來 Kotlin ,快速學(xué)習(xí)Kotlin! 什么是Kotlin? Kotlin 是種靜態(tài)類型編程...
    任半生囂狂閱讀 26,669評論 9 118
  • 心中的花蕾 何為人生呢?有無數(shù)個答案,在我看來,人生就是由生到死的過程,但人生這幅畫卷是否純美,是由自己來決定!有...
    玉壺淚閱讀 600評論 5 4

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