// Constructor 构造模式
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')
// 静态方法
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
}
})()