Skip to content

CSS 盒子模型 链接

标准盒模型 content-box 怪异盒模型 border-box

块级盒子 Block Box & 内联盒子 Inline Box

块级盒子 Block Box:

  • Content box
  • Padding box
  • Border box
  • Margin box

一个被定义成块级的(block)盒子会表现出以下行为:

  • 盒子会在内联的方向上扩展并占据父容器在该方向上的所有可用空间,在绝大数情况下意味着盒子会和父容器一样宽
  • 每个盒子都会换行
  • width 和 height 属性可以发挥作用
  • 内边距(padding), 外边距(margin) 和 边框(border) 会将其他元素从当前盒子周围“推开”

如果一个盒子对外显示为 inline,那么他的行为如下:

  • 盒子不会产生换行。
  • width 和 height 属性将不起作用。
  • 内边距、外边距以及边框会被应用但是不会把其他处于 inline 状态的盒子推开。

display有一个特殊的值,它在内联和块之间提供了一个中间状态。这对于以下情况非常有用:您不希望一个项切换到新行,但希望它可以设定宽度和高度,并避免上面看到的重叠。

一个元素使用 display: inline-block,实现我们需要的块级的部分效果:

  • 设置width 和height 属性会生效。
  • padding, margin, 以及border 会推开其他元素。

翻墙

  1. content-box:标准盒模型,CSS 定义的宽高只包含 content 的宽高
  2. border-box:IE 盒模型,CSS 定义的宽高包括了 content,padding 和 border

Javascript 原型与原型链 链接

普通对象和函数对象

JavaScript 中,万物皆对象!但对象也是有区别的。分为普通对象和函数对象

普通对象函数对象
var o1 = {};function f1(){};
var o2 =new Object();var f2 = function(){};
var o3 = new f1();var f3 = new Function('str','console.log(str)');
console.log(typeof Object); //function
console.log(typeof Function); //function

console.log(typeof f1); //function
console.log(typeof f2); //function
console.log(typeof f3); //function

console.log(typeof o1); //object
console.log(typeof o2); //object
console.log(typeof o3); //object

o1 o2 o3 为普通对象,f1 f2 f3 为函数对象。 凡是通过 new Function() 创建的对象都是函数对象,其他的都是普通对象。f1,f2,归根结底都是通过 new Function()的方式进行创建的。Function Object 也都是通过 New Function()创建的。

构造函数

function Person(name, age, job) {
 this.name = name;
 this.age = age;
 this.job = job;
 this.sayName = function() { alert(this.name) }
}
var person1 = new Person('Zaxlct', 28, 'Software Engineer');
var person2 = new Person('Mick', 23, 'Doctor');

person1 和 person2 都是 Person 的实例。这两个实例都有一个 constructor (构造函数)属性,该属性(是一个指针)指向 Person。 即:

  console.log(person1.constructor == Person); //true
  console.log(person2.constructor == Person); //true

我们要记住两个概念(构造函数,实例): person1 和 person2 都是 (构造函数 Person[函数对象]) 的**实例 ** 一个公式:实例的构造函数属性(constructor)指向构造函数。

原型对象

在 JavaScript 中,每当定义一个对象(函数也是对象)时候,对象中都会包含一些预定义的属性。其中每个函数对象都有一个prototype 属性,这个属性指向函数的原型对象。 **公式:**每个对象都有 proto 属性,但只有函数对象才有 prototype 属性

function Person() {}
Person.prototype.name = 'Zaxlct';
Person.prototype.age  = 28;
Person.prototype.job  = 'Software Engineer';
Person.prototype.sayName = function() {
  alert(this.name);
}

var person1 = new Person();
person1.sayName(); // 'Zaxlct'

var person2 = new Person();
person2.sayName(); // 'Zaxlct'

console.log(person1.sayname == person2.sayname); //true

改写:

Person.prototype = {
   name:  'Zaxlct',
   age: 28,
   job: 'Software Engineer',
   sayName: function() {
     alert(this.name);
   }
}

原型对象就是 Person.prototype 在上面我们给 A 添加了 四个属性:name、age、job、sayName。其实它还有一个默认的属性:constructor

在默认情况下,所有的原型对象都会自动获得一个 constructor(构造函数)属性,这个属性(是一个指针)指向 prototype 属性所在的函数(Person

Person.prototype.constructor == Person
person1.constructor == Person //上边例子中 person1实例

person1 为什么有 constructor 属性?那是因为 person1 是 Person 的实例。 那 Person.prototype 为什么有 constructor 属性??同理, Person.prototype (你把它想象成 A) 也是 Person 的实例。 也就是在 Person 创建的时候,创建了一个它的实例对象并赋值给它的 prototype,基本过程如下:

 var A = new Person();
 Person.prototype = A;

**可以理解为:**原型对象(Person.prototype)是 构造函数(Person)的一个实例。 **结论:**高程红宝书说,构造函数的原型对象并不是构造函数的一个实例。只是构造函数的一个属性,这个属性是自动获得的。

原型对象其实就是普通对象(但 Function.prototype 除外,它是函数对象,但它很特殊,他没有 prototype 属性(前面说道函数对象都有 prototype 属性))。看下面的例子:

function Person(){};
 console.log(Person.prototype) //Person{}
 console.log(typeof Person.prototype) //Object
 console.log(typeof Function.prototype) // Function,这个特殊
 console.log(typeof Object.prototype) // Object
 console.log(typeof Function.prototype.prototype) //undefined

**结论:**所有函数对象的 proto 都指向 Function.prototype,它是一个 javascript native 函数

extend: function(Child, Parent) {
    function Ctor() {
      this.constructor = Child;
    }
    Ctor.prototype = Parent.prototype;
    Child.prototype = new Ctor();
    Child.super_ = Parent.prototype;
  },

target 和 currentTarget 参考

DOM hash

参考 1参考 2