JS 設(shè)計模式

// Constructor 構(gòu)造模式

function A() {
  this.name = 'Vue'
  this.age = 3
}

A.prototype.say = function () {
  console.log('hello')
}

var a = new A()
a.say()

A.prototype.do = function(something) {
  console.log('do ' + something)
}
a.do('work')

// ----------------------------------------------------------

class B {
  constructor(name = 'Vue', age = 3) {
    this.name = name
    this.age = age
  }
  todo() {
    console.log('todo something')
  }
  do(something) {
    console.log('do ' + something)
  }
}
var b = new B()
b.age
b.todo()
b.do('work')

// 靜態(tài)方法
B.say = function() {
  console.log('hello world')
}
// 實例方法
b.hello = function() {
  console.log('hello vue')
}
// -------------------------------------------------------
var a = {
  name: 'Vue',
  age: 3,
  do: function(something = 'work') {
    console.log('do ' + something)
  }
}
a.todo = function() {
  console.log('todo something')
}
// ---------------------------------------------------------
var a = (function() {
  var name = 'Vue'
  var age = 3
  function say() {
    console.log('hello')
  }
  function todo(something) {
    console.log('do ' + something)
  }
  return {
    name: name,
    age: age,
    say: say,
    todo: todo
  }
})()

var b = (function() {
  return {
    num: 0,
    increment() {
      this.num++
    }
  }
})()

var c = (function() {
  var num = 0
  function increment() {
    this.num++
  }
  return {
    num: num,
    increment: increment
  }
})()

var d = (function() {
  var module = {}
  module.num = 0
  module.increment = function() {
    this.num++
  }
  return module
})()

var m = { name: 'Vue' }
var e = (function(module) {
  function hello() {
    console.log(module.name)
  }
  return {
    hello: hello
  }
})(m)

var g = 'React'
var f = (function() {
  function name() {
    console.log(g)
  }
  return {
    name: name
  }
})()
?著作權(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)容

  • 1、寫出 構(gòu)造函數(shù)模式、混合模式、模塊模式、工廠模式、單例模式、發(fā)布訂閱模式的范例。 1、設(shè)計模式分類: 構(gòu)造函數(shù)...
    撫年華輕過閱讀 539評論 0 1
  • 關(guān)鍵詞:類,實例,原型 構(gòu)造函數(shù)定義: 構(gòu)造函數(shù)用于創(chuàng)建特定類型的對象——不僅聲明了使用的對象,構(gòu)造函數(shù)還可以接受...
    RomainLiu閱讀 437評論 0 1
  • *工廠模式factory *構(gòu)造函數(shù)模式constructor *單例模式single *混合模式mixin *模...
    jrg_memo閱讀 394評論 0 0
  • 1.什么叫做設(shè)計模式(基本概念) 在面向?qū)ο筌浖O(shè)計過程中,針對問題進行簡潔而優(yōu)雅的一種解決方案 設(shè)計模式是在某種...
    Jianshu9527閱讀 374評論 0 3
  • 介紹 構(gòu)造函數(shù)大家都很熟悉了,不過如果你是新手,還是有必要來了解一下什么叫構(gòu)造函數(shù)的。構(gòu)造函數(shù)用于創(chuàng)建特定類型的對...
    cbw100閱讀 426評論 1 3

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