如何阻止在创建另一个对象后覆盖原型中的函数的参数
How to block parameters of function which is in prototype overrided after created another object
为了避免相同函数的复制,我听说函数必须在 prototype
中定义。我听说 OOP 继承很糟糕,这就是为什么我在 Javascript 深入研究时尝试使用工厂函数。但是我在下面的代码中遇到了问题。我在 prototype
中定义了所有函数并创建了两个对象,emp
和 emp2
。但是在定义了emp2
之后,emp
的参数被覆盖了。那么工厂函数和原型有什么好处呢?
const eater = (state) => ({
eat(){
console.log(state.name, 'is eating')
}
})
function createEmployee(name, age){
let Employee = Object.create
(createEmployee.prototype)
Employee.name = name
Employee.age = age
Object.assign(
createEmployee.prototype,
eater(Employee)
)
createEmployee.prototype.work = function(){
console.log(Employee.name, 'is working')
}
return Employee
}
let emp = createEmployee('Muzaffer', 24)
emp.work()
emp.eat()
let emp2 = createEmployee('Cuma', 22)
emp.work()
emp.eat()
//Output
//Muzaffer is working
//Muzaffer is eating
//Cuma is working
//Cuma is eating
使用this
关键字时有效
const eater = () => ({
eat(){
console.log(this.name, 'is eating')
}
})
Object.assign(
createEmployee.prototype,
eater()
)
function createEmployee(name, age){
let Employee = Object.create(createEmployee.prototype)
this.name = name
this.age = age
return Object.assign(Employee, this)
}
createEmployee.prototype.work = function(){
console.log(this.name, 'is working')
}
let emp = createEmployee('Muzaffer', 24)
console.log(emp.name)
emp.work()
emp.eat()
let emp2 = createEmployee('Cuma', 22)
console.log('after creating emp2')
emp.work()
emp.eat()
console.log(emp.name)
/*
//Output
Muzaffer
Muzaffer is working
Muzaffer is eating
after creating emp2
Muzaffer is working
Muzaffer is eating
Muzaffer
*/
使用 this
关键字而不使用 new
关键字时不起作用。
"use strict";
const eater = () => ({
eat(){
console.log(this.name, 'is eating')
}
})
Object.assign(
createEmployee.prototype,
eater()
)
function createEmployee(name, age){
let Employee = Object.create(createEmployee.prototype)
this.name = name
this.age = age
return Object.assign(Employee, this)
}
createEmployee.prototype.work = function(){
console.log(this.name, 'is working')
}
let emp = createEmployee('Muzaffer', 24)
console.log(emp.name)
emp.work()
emp.eat()
let emp2 = createEmployee('Cuma', 22)
console.log('after creating emp2')
emp.work()
emp.eat()
console.log(emp.name)
有new
个关键字
const eater = () => ({
eat(){
console.log(this.name, 'is eating')
}
})
Object.assign(
createEmployee.prototype,
eater()
)
function createEmployee(name, age){
this.name = name
this.age = age
}
createEmployee.prototype.work = function(){
console.log(this.name, 'is working')
}
let emp = new createEmployee('Muzaffer', 24)
console.log(emp.name)
emp.work()
emp.eat()
let emp2 = new createEmployee('Cuma', 22)
console.log('after creating emp2')
emp.work()
emp.eat()
console.log(emp.name)
根据这些结果:new
关键字只是使 this
对象的原型引用 createEmployee.prototype
,就像之前的 Object.create
调用一样。和 return this
对象。使用 new
关键字调用函数或仅调用常规函数时 this
对象自动创建。
但是我们不需要在 createEmployee
工厂函数中使用 this
关键字。我们只需要在我们的对象函数中使用 this
关键字,例如 work
和 eat
.
const eater = () => ({
eat(){
console.log(this.name, 'is eating')
}
})
Object.assign(
createEmployee.prototype,
eater()
)
function createEmployee(name, age){
let Employee = Object.create(createEmployee.prototype)
Employee.name = name
Employee.age = age
return Employee
}
createEmployee.prototype.work = function(){
console.log(this.name, 'is working')
}
let emp = createEmployee('Muzaffer', 24)
console.log(emp.name)
emp.work()
emp.eat()
let emp2 = createEmployee('Cuma', 22)
console.log('after creating emp2')
emp.work()
emp.eat()
console.log(emp.name)
为了避免相同函数的复制,我听说函数必须在 prototype
中定义。我听说 OOP 继承很糟糕,这就是为什么我在 Javascript 深入研究时尝试使用工厂函数。但是我在下面的代码中遇到了问题。我在 prototype
中定义了所有函数并创建了两个对象,emp
和 emp2
。但是在定义了emp2
之后,emp
的参数被覆盖了。那么工厂函数和原型有什么好处呢?
const eater = (state) => ({
eat(){
console.log(state.name, 'is eating')
}
})
function createEmployee(name, age){
let Employee = Object.create
(createEmployee.prototype)
Employee.name = name
Employee.age = age
Object.assign(
createEmployee.prototype,
eater(Employee)
)
createEmployee.prototype.work = function(){
console.log(Employee.name, 'is working')
}
return Employee
}
let emp = createEmployee('Muzaffer', 24)
emp.work()
emp.eat()
let emp2 = createEmployee('Cuma', 22)
emp.work()
emp.eat()
//Output
//Muzaffer is working
//Muzaffer is eating
//Cuma is working
//Cuma is eating
使用this
关键字时有效
const eater = () => ({
eat(){
console.log(this.name, 'is eating')
}
})
Object.assign(
createEmployee.prototype,
eater()
)
function createEmployee(name, age){
let Employee = Object.create(createEmployee.prototype)
this.name = name
this.age = age
return Object.assign(Employee, this)
}
createEmployee.prototype.work = function(){
console.log(this.name, 'is working')
}
let emp = createEmployee('Muzaffer', 24)
console.log(emp.name)
emp.work()
emp.eat()
let emp2 = createEmployee('Cuma', 22)
console.log('after creating emp2')
emp.work()
emp.eat()
console.log(emp.name)
/*
//Output
Muzaffer
Muzaffer is working
Muzaffer is eating
after creating emp2
Muzaffer is working
Muzaffer is eating
Muzaffer
*/
使用 this
关键字而不使用 new
关键字时不起作用。
"use strict";
const eater = () => ({
eat(){
console.log(this.name, 'is eating')
}
})
Object.assign(
createEmployee.prototype,
eater()
)
function createEmployee(name, age){
let Employee = Object.create(createEmployee.prototype)
this.name = name
this.age = age
return Object.assign(Employee, this)
}
createEmployee.prototype.work = function(){
console.log(this.name, 'is working')
}
let emp = createEmployee('Muzaffer', 24)
console.log(emp.name)
emp.work()
emp.eat()
let emp2 = createEmployee('Cuma', 22)
console.log('after creating emp2')
emp.work()
emp.eat()
console.log(emp.name)
有new
个关键字
const eater = () => ({
eat(){
console.log(this.name, 'is eating')
}
})
Object.assign(
createEmployee.prototype,
eater()
)
function createEmployee(name, age){
this.name = name
this.age = age
}
createEmployee.prototype.work = function(){
console.log(this.name, 'is working')
}
let emp = new createEmployee('Muzaffer', 24)
console.log(emp.name)
emp.work()
emp.eat()
let emp2 = new createEmployee('Cuma', 22)
console.log('after creating emp2')
emp.work()
emp.eat()
console.log(emp.name)
根据这些结果:new
关键字只是使 this
对象的原型引用 createEmployee.prototype
,就像之前的 Object.create
调用一样。和 return this
对象。使用 new
关键字调用函数或仅调用常规函数时 this
对象自动创建。
但是我们不需要在 createEmployee
工厂函数中使用 this
关键字。我们只需要在我们的对象函数中使用 this
关键字,例如 work
和 eat
.
const eater = () => ({
eat(){
console.log(this.name, 'is eating')
}
})
Object.assign(
createEmployee.prototype,
eater()
)
function createEmployee(name, age){
let Employee = Object.create(createEmployee.prototype)
Employee.name = name
Employee.age = age
return Employee
}
createEmployee.prototype.work = function(){
console.log(this.name, 'is working')
}
let emp = createEmployee('Muzaffer', 24)
console.log(emp.name)
emp.work()
emp.eat()
let emp2 = createEmployee('Cuma', 22)
console.log('after creating emp2')
emp.work()
emp.eat()
console.log(emp.name)