function iConstructor(tag, href){
if(arguments.length > 1){
this.tag = arguments[0]
this.href = arguments[1]
}
}
const instance = new iConstructor()
iConstructor.prototype.r = function(){
// do something with the object
// iConsttructor's attribute here
}
about primitive type:
Number
, Boolean
, String
,null
and undefined
(sec. 3.4
all of which are immutable
technically speaking only objects have methods,
but Number
, Boolean
, String
also do
(‘cause they can be wrapped
about object type:
defination:
object is the collection of property,
every property consists of a name/value,
class object:
collection of objects defined by constructor funtion
(some common class object defined by JavaScript:
Array
, Function
(VIO), Date
, RegExp
, Error
Global object
JavaScript解释器启动时会创建一个全局对象,并给这个对象一组定义的初始属性,主要包括:
全局属性,如undefined
, Infinity
, NaN
全局函数,如isNaN()
, parseInt
, eval()
构造函数,如Date()
, RegExp()
, String()
, Array()
, Object()
全局对象,如Math
, JSON
在最上层(所有函数体以外)可以通过this
引用它
(ES2015以上有所区别,check about it later)
more about type convert: sec. 3.8
// a function, execute when the page finished loading
window.onload
// create an element taged with String arguments
document.createElement
// some DOM operation:
eleme2Add.appendTo(elemeAdded2)
window.localStorage
通过if()
, &&
, type convert判断是否会引起undefined或者type error再调用
if(image.addEventHandler)
image.addEventHandler(...)
else
// ie
image.attachEvent(..)
// ******saved input in localStorage******
const inputBars = document.getElementsByTagName("input")
function save(){
if(window.localStorage && localStorage.SOMETHING)
for(int i = 0; i < inputBars.length-1; i++)
SOMETHING[inputBars[i].name] = inputBars[i].value
}
window.onload = function(){
if(window.localStorage && localStorage.SOMETHING)
for(int i = 0; i < inputBars.length-1; i++){
if(SOMETHING[inputBars[i].name])
inputBars[i].value = SOMETHING[inputBars[i].name]
}
}
optional divides ‘,’
JavaScript并不是在所有换行处都填补分号,而是没有分号就无法解析的时候才填补分号
e.g. :
const y = x + f
(a+b).toString()
// => const y = x + f(a+b).toString();
也有例外:
return, break, continue 以及 ++, --
声明放在函数体头部以清晰标明作用范围
just sth about CSS class
var list = document.querySelector('ul');
list.addEventListener('click', function(ev) {
if( ev.target.tagName === 'LI') {
ev.target.classList.toggle('done');
}
}, false);
小数比较
JavaScript使用 IEEE-754 浮点数表示法(一种二进制表示法
1/2, 1/4, 1/1024 都可以精确表示
但是1/10, 1/1000 这样的十进制分数无法精确表示,只能表示近似值
所以才有这个很出名的例子:
const f1 = .3 - .2
const f2 = .2 - .1
f1 == f2 // => false
f1 == .1 // => false
f2 == .1 // => true
其实值非常接近,这种情况也只在比较时才会出现
解决办法: 涉及金融计算尽量用整数避免使用小数
值转换成boolean类型
只有 undefined
, null
, 0
, -0
, NaN
, ""
(也被统称为falsy value)会被转换成 false
.
所有其他值,包括所有对象都会转换成 true
null & undefined
null
是关键字,用于表示数字,串,对象值只为空,对其执行typeof
返回'object'
undefined
不是关键字,表示变量未初始化,对其执行typeof
返回'undefined'
null == undefined // => true , 应该用 === 来区分它们
包装对象
通过.
调用原生类型的属性时,JavaScript会通过对应构造方法创建一个class对象来执行,这个临时对象只供读取,赋值会被忽略.
类型转换
值|(转为:)字符串|数字|布尔值|对象 —|—|—|—|— undefined|”undefined”|NaN|fasle|throws TypeError null|”null”|0|false|throws TypeError true|”true”|1||new Boolean(true) false|”false”|0||new Boolean(false) ““(空字符串)||0|false|new String(“”) NaN|”NaN”||false|new Number(NaN) Infinity|”Infinity”||true|new Number(Infinity) [] » (任意数组)|”“|0|true| [9]|”9”|9|true| [‘a’]|调用join|NaN|true Number to String & String to Number
const x = 100.129
x.toString(10) // 转成10进制
x.toFixed(2) // => 100.13 精确到小数点后2位
x.toExponential(2) // => 1.00E2 科学计数法小数点后2位
x.toPrecision(5) // => 100.13 5位有效数字
Number('100.129') // 参数仅限十进制,不能有非法尾随字符
parseInt(' 1001.29 sd gundam ') // => 1 忽略左起任意位空格,识别连续的尽可能多的数字,忽略后面的内容
parseInt('.134') // => NaN 第一个非空格不是数字直接返回NaN
Boolean(new Boolean(false)) // => true
Object to Number & String
通过valueOf()
和 toString()
从对象向数字,字符串转化(Date是特例)
( to number时,若valueOf()
返回值不是原生值,调用toString()
,仍然不是,throws Type Error
( to string时,若toString()
返回值不是原生值,调用valueOf()
,仍然不是,throws Type Error
+
, ==
, !=
运算符会执行这种对象到原始值的转化,其他运算符比较明确
Function Scope, Hoisting(声明提前)
as the titles indicates…
Scope Chain:
每一段JavaScript代码都有一个与之关联的作用域.
作用域链是一个对象列表或链表,这组对象定义了这段代码”作用域中”的变量.
当JavaScript需要查找变量x的值时(variable resolution), 它会从链中的第一个对象开始查找, 找到则使用, 没找到则下一个…
defining a function will save a scope chain.
calling a func will create a new obj to store local variables of the func and add it to the chain, then create a new longer chain(function invocation scope).
执行优先级,结合性
.
(属性访问表达式,调用表达式)优先级高于一切
P78查看详细…
关系表达式
==
& !==
(相等与不等运算符)
NaN
与任何对象都不等,包括自己 (判断是否为NaN
: x!==x
,isNaN()
两字符串内容相同编码不同,==
和===
判定为不等
>
, <
, >=
, <=
(比较运算符)
两个字符串:比较unicode字符索引
有一个是NaN(或转化成NaN):false
+
倾向于字符串,操作数有一个String就做连接操作,
而比较运算符倾向于数字,两个操作数均为String才比较字符串
in
, instanceof
, 原型链
*
**关于eval
eval(“2+3”) // => 5
可改变局部变量
|直接调用(eval()
)|间接调用(geval = eval
)
—|—|—
作用域|调用eval()的上下文作用域|全局对象(无法读,写,定义局部变量,函数)
全局eval()
可用于执行对上下文无依赖的全局脚本代码段
关于标签语句
identifier: statements
语句的标签,可以在别处引用(break
, continue
跳转
also known as ‘hash’, ‘hashtable’, ‘dictionary’, ‘associative array’
prototypal inheritance, prototype chain
Property Type
How To Create
inherite(p){
if(p == null) throw TypeError()
if(Object.create) return Object.create(p)
var t = typeof p
if( t !== 'object' && t !== 'function') throw TypeError()
function f(){}
f.prototype = p
return new f()
}
Get and Set
.
, []
in
» hasOwnProperty()
» propertyIsEnumerable()
for(in)
» Object.keys()
(based on for(in)
) » Object.getOwnPropertyNames()
{ get accessor_prop(){ /* do something */ }; }
// 直接量
Object.defineProperty(obj, propName, propDesciptorObj)
// define oneObject.defineProperties(objID, {propName: propDescriptor})
// define a lotTalk is Cheap, Show Me The Code
Object.defineProperty(Object.prototype, 'deepClone', {
writable: true,
configurable: false,
extensible: true,
value: function(o){
var p = {}
var names = Object.getOwnPropertyNames(o)
for(var desc, int i = 0; i < names.length; i++){
// if(names[i] in p) continue; // don't know if it will help
desc = Object.getOwnPropertyDescriptor(names[i])
Object.defineProperty(p, names[i], desc)
}
return p
}
})
关于Prototype
Object.create(Object.prototype)
obj.constructor.prototype // 获取obj的prototype (ES3 ver.
Object.getPrototypeOf(obj) // 获取obj的prototype (ES5 ver.
proto_obj.isPrototypeOf(obj) // obj是否在proto_obj原型链上 (作用类似instanceof运算符
__proto__ // Mozilla的JavaScript对外暴露了这个用于直接查询/设置prototype
关于class
可以通过调用Object.prototype.toString.call(obj)
来显示obj的class attribute, 通常为'[object SOMETHING]' // .slice(8, -1)即类名SOMETHING
关于CORS & XSS
how to call it
this
from one context to another.window
this
In most case, the value of this
in a func depends on how a function is called, and it can’t be set by assignment during execution.
In ES5, there is bind()
to set val of this regardless off how it’s called
In ES2015, there is arrow function(=>
) which don’t provide their this
binding and retains the value of its enclosing lexical context.
simple call
=> if(use strict) this === what it’s setted, undefined
by default.
else this === its enclosing lexical context.
arrow function
// Create obj with a method bar that returns a function that
// returns its this. The returned function is created as
// an arrow function, so its this is permanently bound to the
// this of its enclosing function. The value of bar can be set
// in the call, which in turn sets the value of the
// returned function.
var obj = {
bar: function() {
var x = (() => this);
return x;
}
};
// Call bar as a method of obj, setting its this to obj
// Assign a reference to the returned function to fn
var fn = obj.bar();
// Call fn without setting this, would normally default
// to the global object or undefined in strict mode
console.log(fn() === obj); // true
// But caution if you reference the method of obj without calling it
var fn2 = obj.bar;
// Then calling the arrow function this is equals to window because it follows the this from bar.
console.log(fn2()() == window); // true
//how about call obj.bar()() directly?
//
console.log(obj.bar()() === obj) // true
called as method
when called as a method, its this
will be set to the object the method is called on, wherever the func is defined.
so it is if called as getter, setter or inherited from prototype chain.
but as a constructor, its this
will be the created object.
called as eventHandler
this
refers to the element the event fired from.
while inline on-eventHandler
’s this
is set to the element on which it placed on.
arguments, caller & callee
arguments
is a special array object, has access to the arguments with argumens[index]
caller
& callee
is current func caller & the called func
var factoria = function(x){
if(x===1) return x
return arguments.callee(x-1)
}
defined func props
function
is kind of special object,
thus can has its own props
function factoria(n){
if(Number.isFinite(n) && n > 0 && n ){
factoria[n] = n * factoria[n-1]
return factoria[n]
}
}
factoria[1] = 1
some functional styles
// some func supposed to be called immediately
const val = (function (x){return x * x}(10))
// attention to the ( before 'function', it's required
thoughts on distraction
感觉如今相比几十年前娱乐方式太多了,也许更多的人因此被分散了注意力,个体从事研究的时间和效率总体来说降低了。从这个角度来说游戏,社交网络这些是否对科技进步的影响负面居多?(也许靠堆人数所以总体呈现还是在加速…)(也许只是我庸人自扰多虑了…)
由”所有我所能想到的课题一定有前人做过了”定律可得:
也许根本不存在这种客观指标, 干扰因素太多了…(有
左右两边是不同单位的y轴,x轴为时间(年),中间是不同颜色的曲线连接统计数据的那个图叫啥…
WHICH ONE IS BETTER
Here are different pieces of CSS, in roughly the order I found most helpful to learn them, although the steps will overlap a bit.
Selection
know your HTML elements and attributes really well. You will not understand CSS without a good knowledge of HTML. Understanding the tree structure of HTML, the element names, the difference between ids and classes will help you know how to target your styles. Try to structure your HTML so you have to use as few selectors as possible in your CSS, because more selectors require more browser processing and, worse, make it really difficult to override styles for special cases or theme variants.
Basic layout
know the old school forms of layout: the box model, the difference between block and inline and inline-block, and how these affect padding, border, margin and width. Learn about floats, and when you would want to use them instead of inline block for example. Learn about positioning, especially relative, static, and absolute (fixed is more rare). Also learn the newer flexbox model: it’s sometimes quirky (the last row of justified content is a travesty), but it also can come in handy and save you some of the work in creating a layout yourself in floats, for example. A good exercise is to try to recreate the layout of reasonable complex sites yourself with lots of columns and different types of lists. A good exercise is creative a dropdown menu with only CSS, or a 5 star-rating widget.
Get familiar with other basic rules.
Rules that aren’t layout are usually pretty straight forward to understand (although animation is a little more complex). Get to know how to style backgrounds, type, shadows, transformations, etc. Lear how to style elements using the fewest possible images to reduce browser load times: when you can use CSS alone and what you have to use images for, how to crop and optimize images and sprite images.
Check out the differences between browsers.
This is most noticeable in form styling–browsers differ greatly not only in default styles for forms, but even in the style rules they’ll let you apply to various elements, even which elements they support at all.
Learn the hacks/tricks.
There are a lot of common problems solved over and over again in what you might as a beginner think are slightly tricky ways. One example, which comes up less often than it used to, is when you have to use an image instead of text, how do you keep the actual text on the page for search engines and screen readers? How do you deal with images of different resolutions is a newer problem.
When you understand the fundamentals, start with the advanced tools.
Learn about CSS preprocessors like Less and Sass, and how to use them efficiently so you don’t end up with ridiculous amounts of code bloat. Learn how to concatenate and minify style sheets, and use automatic spriters and image optimizers. All of these tools are great, but learning the fundamentals yourself first will help you have a thorough knowledge of how the more advanced tools work.
[attr[~^$*]=val]
data-
also accessible in JS via el.dataset.camelAttr
el:kindaState
:visited
, :active
, :focus
, :hover
, :checked
, :required
…:lang()
, :nth-child(odd):nth-child(-n+15)
, :dir()
, :matches()
, :not()
, :nth-last-child()
, :nth-last-of-type()
, :nth-of-type()
el::kindaPostion
::before
, ::after
, ::first-letter
, ::first-line
, ::selection
, ::backdrop
:nth-child( <nth> [ of <complex-selector-list> ]? )
where
<nth> = <an-plus-b> | even | odd
<complex-selector-list> = <complex-selector>#
where
<complex-selector> = <compound-selector> [ <combinator>? <compound-selector> ]*
where
<compound-selector> = [ <type-selector>? <subclass-selector>* [ <pseudo-element-selector> <pseudo-class-selector>* ]* ]!
<combinator> = '>' | '+' | '~' | [ '||' ]
where
<type-selector> = <wq-name> | <ns-prefix>? '*'
<subclass-selector> = <id-selector> | <class-selector> | <attribute-selector> | <pseudo-class-selector>
<pseudo-element-selector> = ':' <pseudo-class-selector>
<pseudo-class-selector> = ':' <ident-token> | ':' <function-token> <any-value> ')'
where
<wq-name> = <ns-prefix>? <ident-token>
<ns-prefix> = [ <ident-token> | '*' ]? |
<id-selector> = <hash-token>
<class-selector> = '.' <ident-token>
<attribute-selector> = '[' <wq-name> ']' | '[' <wq-name> <attr-matcher> [ <string-token> | <ident-token> ] <attr-modifier>? ']'
where
<attr-matcher> = [ '~' | | | '^' | '$' | '*' ]? '='
<attr-modifier> = i
!importance
after a prop pairselector|score(forEach) —|— element & psudo-element|1 class, attribute & psudo-class|10 id|100 inline style props|1000
>
, +
, ~
, ` ),
* and
:not`has no effect on specificity.some property values applied to an element will be inherited by that element’s children, and some won’t. Which prop inherites by default and which don’t is down to common sense (mag, pad, bg-img, font, color for example)
Controlling inheritance
inherit
initial
unset
revert
display: float;
how to clear it
.container::after {clear: left | right | both;}
.container {overflow: auto; display:flow-root }
(and some other tricks1:
:first-child::before {display: table}
will prevent first-child’s margin-top from combine, or collapse2
position: static
(by default in ‘normal flow’
other options include:
relative
: relative to static position;
absolute
: relative to window, or container( if an ancestor ele isn’t static, relative
for example;
fixed
: relative to window, or container( if ancestor’s transform
|perspective
|filter
setted, stay where it is;
sticky
: relative to window, act like static, but fixed when moved to certain position;
视觉盒子模型将页面元素转化成0,1或多个符合CSS box model的box, 每个box被下列因素所定义:
a BFC 是一个网页CSS渲染的一部分. 也是块级盒子布局出现, float元素和其他元素交互的区域
BFC由以下方式创建:
一些后来觉得答得不是很好的问题
想过好几次这个问题该怎么回答, 我就直说了
(你塔玛不熟就别写上去…
(这些概念都不知道你面个🐔🚌前端, 你可以滚了