new 原理和实现
Open
Nobody has claimed this yet.
- Dominant language
- HTML
- Stars
- 6
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
步骤
1.创建一个空对象
2.把对象的__proto__属性指向父类的prototype,空对象将拥有父类的prototype的属性
3.执行构造函数,并且把this指向创建的对象,并且父类里面this的赋值(父类的属性)将赋值到新创建的对象。
4.返回对象
function Base() {
this.id = 'base';
}
Base.prototype.toString = function() {
return this.id;
};
var obj = new Base();
创建空对象{},obj
把obj的__proto__属性赋值到Base的prototype上面。
执行Base.call(obj,arguments),把this指向obj,把Base的属性(this的赋值)作用到obj上面
返回对象
function base(name,age) {
this.name=name
this.age=age
}
base.prototype.sayName=function(){
console.log(this.name)
}
function newFn(fn,...arg){
let obj={}
obj.__proto__=fn.prototype
fn.call(obj,...arg)
return obj
}
let obj1=newFn(base,'chen',31)
let obj2=newFn(base,'lu',33)
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with issue #36 and review the JavaScript examples for Base, base, and newFn. The issue names no repository file or test and does not define a specific documentation change, so first locate the project's documentation entry point and confirm the intended scope before proceeding.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100