一个前端狗的自我成长之路,目的这种是为了记录学习路上遇到的问题。同时避免面试的时候 keywords 式的回答
leemiaoshun / notes Goto Github PK
View Code? Open in Web Editor NEW一个前端狗的自我成长之路
一个前端狗的自我成长之路
一个前端狗的自我成长之路,目的这种是为了记录学习路上遇到的问题。同时避免面试的时候 keywords 式的回答
其实题目这么来讲的话,你会从什么角度去思考呢?
比如 var bar = {}
这算不算一种方法?但是这个样子声明对象其实是语法糖。
function Person (name){
var bar = new Object();
bar. a = name;
bar.say = function(){
console.log(this.name);
}
return bar
}
var person = new Person('jser');
缺点所有对象原型指向一个原型 无法识别对象
funciton Person(name){
this.name = name;
this.sayName = funciton(){
console.log(this.name);
}
// 没有显示创建对象
// 没有return
}
var person = new Person('jser');
缺点每次创建实例的时候,方法都会被创造一遍 *
优点实例可以被识别为一个特别模型
function Person (){}
Perosn.prototype.name = 'jser';
Person.prototype.sayName = function(){
console.log(this.name);
}
var person = new Person()
方法不会被重复创建
但是不能初始化参数,并且方法会被所有实例共享
function Person (name){
this.name = name;
}
Person.prototype.sayName = function(){
console.log(this.name);
}
var person = new Person();
取了二者的优点,方法不会被多次重新创建,并且可以传参数。方法被所有实例共享。该共有共有,该私有私有
function Person(name){
var bar = new Object();
bar.name = name;
bar.sayName = function(){
console.log(this.name)
}
return bar;
}
var person = new Person('jser');
其实跟工厂模式差不多。
无法使用contractor或instanceof识别对象实例的类型。因为都来自 object
function Person(name){
var obj = new Object();
bar.name = name;
bar.sayName = function(){
console.log(this.name)
}
return obj;
}
新创建的实例不引用 this 不使用 new 操作符创建实例
将变量私有化
同样无法使用 contractor 和 instanceof 识别对象实例的类型,
讲解同样需要从一个问题开始
add(1)(2)(3)() // 输出6
// 请完善 add 函数
function add(i) {
const result = 0;
const resultList = []
resultList.push(i)
const fn = (...args) => {
if(args.length===0) {
return resultList.reduce((prev,next)=>{
return prev + next
},0)
}
resultList.push(args[0])
return fn
}
return fn
}
console.log(add(1)(2)(3)())
先说答案,再来详细解释
function Parent(){
this.name = 'jser';
this.color = ['1','2'];
}
Parent.prototype.sayName = function () {
console.log(this.name);
}
function Child() {
this.subName = 'jserrrrrrr'
}
// 核心代码 改写子函数的原型链
Child.prototype = new Parent();
const c1 = new Child();
const c2 = new Child();
c1.color.push("new");
c1.sayName();
console.log(c1.color);
console.log(c2.color);
我们可以发现,原型连方式继承,会发生多个实例共享同一个原型,他们会互相影响。
function Parent(name){
this.name = name;
this.color = ['1','2','3'];
}
Parent.prototype.getName = function(){
console.log(this.name);
}
function Child(name,age){
// 核心代码
Parent.call(this,name);
this.age = age;
}
const c1 = new Child('jser','12');
const c2 = new Child('jser1','22');
console.log(c1)
console.log(c2)
function Parent(name){
this.name = name;
this.color = [1,2,3];
}
Parent.prototype.sayName = function (){
console.log(this.name);
}
function Child(name,age){
// 核心代码1
Parent.call(this,name);
this.age = age;
}
// 核心代码2
Child.prototype = new Parent();
Child.contructor = Child; // contructor设置为构造函数本身
const c1 = new Child('jser',1111);
const c2 = new Child('jser2',2222);
c1.sayName();
c2.color.push('123123');
console.log(c1.color)
console.log(c2.color);
借用 object create 方法
var person = {
name:'jser',
color:[1,2,3]
}
var p1 = Object.create(person);
var p2 = Object.create(person);
p1.name = 'jser1';
p2.name = 'jser2';
p1.color.push('1213123');
console.log(p1);
console.log(p2);
function Parent(original){
var clone = Object.create(original);
clone.sayName = function(){
console.log(this.name);
}
return clone;
}
var p1 = Parent({
name:'jser',
color:[1,2,3]
})
var p2 = Parent({
name:'jser2',
color:[1,2,3]
})
最常用的构造式原型链继承和构造函数的组合方法,这种方式继承会调用两次父函数,第二遍用来改写原型链,有没有办法可以不调用直接改写呢,
比如直接 Child.prototype = Parent.prototype 这样子做的后果就是 可能在修改子函数的原型的时候无意修改到父函数的原型。所以我们不能用这个方法
我们可以借用一个方法
Object。create 实现,具体语法请看这里 我叫链接
function Parent(name){
this.name = name;
this.color = [1,2,3];
}
Parent.prototype.sayName = function (){
console.log(this.name);
}
function Child(name,age){
// 核心代码1
Parent.call(this,name);
this.age = age;
}
// 核心代码2
Child.prototype = Object.create(Parent.prototype);
Child.contructor = Child; // contructor设置为构造函数本身
const c1 = new Child('jser',1111);
const c2 = new Child('jser2',2222);
c1.sayName();
c2.color.push('123123');
console.log(c1.color)
console.log(c2.color);
故事的开始要从一个题目开始讲起
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
<style>
* {
padding: 0;
margin: 0;
}
.container {
width: 600px;
height: 300px;
display: flex;
}
.left {
flex: 1 2 300px;
background: red;
}
.right {
flex: 2 1 200px;
background: blue;
}
</style>
求上述代码中的 left
和 right
的实际宽度
问题的解决必须从flex的属性开始说起
其实 flex 是一个缩写
flex属性是flex-grow,flex-shrink和flex-basis的缩写。
语法
flex: none | auto [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
默认值 flex 1 其实是 flex 0 1 auto 的意思
讲清楚了三个属性的意思,那么就很好记算了。
当前空间还有剩余,剩余为 100px 其中 left占比为 三分之一 right 为 三分之二 答案也就出来了
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.