chenhuiYj / chenhuiYj/note

new 原理和实现

Open
#36 0 comments 0 reactions 0 assignees View on GitHub

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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.