javascript 更好的JS-oop.js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 更好的JS-oop.js相关的知识,希望对你有一定的参考价值。

// constructor function implementation
function Test (args) {
  // explicit internal state without using `this`
  const state = {
    op: args.op,
    missing: args.missing
  }
  return { // explicit return
    // explicit binding of function
    // parameters with internal state
    // or only whatever it needs
    go: go.bind(null, state)
  }
}

// functions defined separately
// can't (and shouldn't) alter object state
// should only act on inputs and return
function go (options) {
  return `${options.op} without: ${options.missing}`
}

// during normal use
const options = {op: 'running', missing: 'this'}
const test = Test(options) // `new` is optional
console.log('Normal use and', test.go())

// testing the function directly
const testOptions = {op: 'Testing', missing: 'object'}
const expected = 'Testing without: object'
const actual = go(testOptions)
console.log(actual)
console.log('Test passes:', actual === expected)

以上是关于javascript 更好的JS-oop.js的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 更好的Javascript键盘导航

获取 Javascript 变量类型的更好方法?

哪个 javascript 缩小库产生更好的结果? [关闭]

更好地理解 javascript 预编译

javascript 更好的光滑箭头光滑禁用

javascript 更好的图像适应