Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
webVueBlog
Leetcode
Repository navigation
Code
Issues
84
(84)
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Leetcode
/
js-code
/
js
/
new.js
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
34 lines (31 loc) · 1.33 KB
master
Breadcrumbs
Leetcode
/
js-code
/
js
/
new.js
Copy path
Top
File metadata and controls
Code
Blame
34 lines (31 loc) · 1.33 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function objectFactory(){
// 1. 首先创建了一个新的空对象
let newObject = null;
let constructor = Array.prototype.shift.call(arguments); // 类数组头部
let result = null;
// 判断参数是否是一个函数
if(typeof constructor !== 'function') {
console.error('type error');
return;
}
// 2.设置原型,将 对象的原型 设置为 函数的prototype对象
// 新建一个空对象,对象的原型 为 构造函数的prototype对象
newObject = Object.create(constructor.prototype);
// 3.让函数的this指向这个对象,执行构造函数的代码(为这个新对象添加属性)
// 将this指向新建对象,并执行函数
result = constructor.apply(newObject, arguments);
// 4.判断函数的返回值类型,如果是值类型,返回创建的对象。如果是引用类型,就返回这个引用类型的对象。
let flag = result && (typeof result === 'object') || typeof result === 'function';
// 判断返回结果
return flag ? result : newObject;
}
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const car1 = new Car('Eagle', 'Talon TSi', 1993);
// console.log(car1);
// Car { make: 'Eagle', model: 'Talon TSi', year: 1993 }
console.log(objectFactory(Car, 'Eagle', 'Talon TSi', 1993))
// Car { make: 'Eagle', model: 'Talon TSi', year: 1993 }
You can’t perform that action at this time.