Comments (6)
delete操作符只能对实例的属性和方法有效,对原型上的属性和方法删除无效
es5引入了几个方法来防止对对象的修改:
- 防止扩展
禁止为对象添加属性和方法,但已存在的属性和方法是可以被修改和删除 - 密封
类似‘防止扩展’,而且禁止为对象删除已存在的属性和方法 - 冻结
类似‘密封’,而且禁止为对象修改已存在的属性和方法(所有字段均只读)
//防止扩展
var person = {name:'nico'}
Object.preventExtensions(person);
console.log(Object.isExtensible(person)); //false
person.age = 22; //正常情况下,悄悄失败,严格模式下会抛出错误
//密封
Object.seal(person);
console.log(Object.isExtensible(person)); //false
console.log(Object.isSealed(person)); //true
delete person.name; //正常情况下,悄悄失败,严格模式下会抛出错误
person.age = 22; //同上
//冻结
Object.freeze(person);
console.log(Object.isExtensible(person)); //false
console.log(Object.isSealed(person)); //true
console.log(Ojbect.isFrozen(person)); //true
person.name = 'welcome'; //正常情况下,悄悄失败,严格模式下会抛出错误
person.age = 22; //同上
delete person.name; //同上
from notebook.
var name = 'some';
var adding = {
name: 'adding',
say: function(){ alert('I\'m ' + this.name); }
};
adding.say(); //I'm adding
setTimeout(adding.say, 1000); //I'm some
setInterval(adding.say, 1000); //I'm some
setTimeout(function(){
adding.say(); //I'm adding
}, 1000);
setInterval(function(){
adding.say(); //I'm adding
}, 1000);
2
在setTimeout中传入的不是函数时,this则指向当前对象;
当在setTimeout中传入的参数为函数时,函数内部的this才会指向window对象。
可以通过bind来绑定this值
var name = 'some';
var adding = {
name: 'adding',
say: function(){ setTimeout(console.log('i\'m ' + this.name), 1000); }
};
adding.say(); // i'm adding
var adding = {
name: 'adding',
say: function(){ setTimeout(function(){
console.log('i\'m ' + this.name);
},1000)
}
};
adding.say(); // i'm some
可以通过保存this值或bind方法来得到我们期望的this
如果使用call或apply方法来代替bind方法,得到的结果也是正确的,但是call方法会在调用之后立即执行,那样也就没有了延时的效果,定时器也就没有用了
setTimeout/setInterval方法的第三个参数及之后的参数都作为function函数的参数
var timeoutID = scope.setTimeout(function[, delay, param1, param2, ...]);
var timeoutID = scope.setTimeout(function[, delay]);
var timeoutID = scope.setTimeout(code[, delay]);
from notebook.
避免使用空位数组
new Array(3)
构造函数会返回一个 length 属性被设置为此参数的空数组。需要特别注意的是,此时只有 length 属性被设置,真正的数组并没有生成。
new Array(3).join('#')
将会返回 ##
javascript-garden
array 空位
from notebook.
attribute & property
attribute
是对于html而言的特性,property
是对于dom节点而言的;对于标准的html特性,可以通过点操作符来获取值,而非标准的html特性,需要通过方法来获取值,比如getAttribute
;
attribute & property这篇文章介绍了两者的共同点和不同点,如通过DOM对象的点操作符获取非标准的html特性,值会等于undefined
;
<body id="test" something="non-standard">
<script>
alert(document.body.id); // test
// non-standard attribute does not yield a property
alert(document.body.something); // undefined
</script>
</body>
对于标准的html特性的更新,对应的dom获取的property
值也会相应更新
<input>
<script>
let input = document.querySelector('input');
// attribute => property
input.setAttribute('id', 'id');
alert(input.id); // id (updated)
// property => attribute
input.id = 'newId';
alert(input.getAttribute('id')); // newId (updated)
</script>
But,input
的value
值不会同步更新
<input>
<script>
let input = document.querySelector('input');
// attribute => property
input.setAttribute('value', 'text');
alert(input.value); // text
// NOT property => attribute
input.value = 'newValue';
alert(input.getAttribute('value')); // text (not updated!)
</script>
对于一些非标准的attribute
,一般使用data-*
前缀,这样就可以通过DOM对象的点操作符来获取/更新值,如
<div id="order" class="order" data-order-state="new">
A new order.
</div>
<script>
// read
alert(order.dataset.orderState); // new
// modify
order.dataset.orderState = "pending"; // (*)
</script>
from notebook.
javascript 运算符的优先级
Precedence | Operator type | Associativity | Individual operators |
---|---|---|---|
20 | Grouping |
n/a | ( … ) |
19 | Member Access |
left-to-right | … . … |
Computed Member Access |
left-to-right | … [ … ] |
|
new (with argument list) |
n/a | new … ( … ) |
|
Function Call | left-to-right | … ( … ) |
|
18 | new (without argument list) |
right-to-left | new … |
17 | Postfix Increment |
n/a | … ++ |
Postfix Decrement |
… -- |
||
16 | Logical NOT | right-to-left | ! … |
Bitwise NOT | ~ … |
||
Unary Plus | + … |
||
Unary Negation | - … |
||
Prefix Increment | ++ … |
||
Prefix Decrement | -- … |
||
typeof | typeof … |
||
void | void … |
||
delete | delete … |
||
await | await … |
||
15 | Exponentiation | right-to-left | … ** … |
14 | Multiplication | left-to-right | … * … |
Division | … / … |
||
Remainder | … % … |
||
13 | Addition | left-to-right | … + … |
Subtraction | … - … |
||
12 | Bitwise Left Shift | left-to-right | … << … |
Bitwise Right Shift | … >> … |
||
Bitwise Unsigned Right Shift | … >>> … |
||
11 | Less Than | left-to-right | … < … |
Less Than Or Equal | … <= … |
||
Greater Than | … > … |
||
Greater Than Or Equal | … >= … |
||
in | … in … |
||
instanceof | … instanceof … |
||
10 | Equality | left-to-right | … == … |
Inequality | … != … |
||
Strict Equality | … === … |
||
Strict Inequality | … !== … |
||
9 | Bitwise AND | left-to-right | … & … |
8 | Bitwise XOR | left-to-right | … ^ … |
7 | Bitwise OR | left-to-right | … | … |
6 | Logical AND | left-to-right | … && … |
5 | Logical OR | left-to-right | … || … |
4 | Conditional | right-to-left | … ? … : … |
3 | Assignment | right-to-left | … = … |
… += … |
|||
… -= … |
|||
… **= … |
|||
… *= … |
|||
… /= … |
|||
… %= … |
|||
… <<= … |
|||
… >>= … |
|||
… >>>= … |
|||
… &= … |
|||
… ^= … |
|||
… |= … |
|||
2 | yield | right-to-left | yield … |
yield* | yield* … |
||
1 | Comma / Sequence | left-to-right | … , … |
from notebook.
相等(==)深入解析
The comparison x == y, where x and y are values, produces true or false. Such a comparison is performed as follows:
- If Type(x) is the same as Type(y), then
- If Type(x) is Undefined, return true.
- If Type(x) is Null, return true.
- If Type(x) is Number, then
- If x is NaN, return false.
- If y is NaN, return false.
- If x is the same Number value as y, return true.
- If x is +0 and y is −0, return true.
- If x is −0 and y is +0, return true.
- Return false.
- If Type(x) is String, then return true if x and y are exactly the same sequence of characters (same length and same characters in corresponding positions). Otherwise, return false.
- If Type(x) is Boolean, return true if x and y are both true or both false. Otherwise, return false.
- Return true if x and y refer to the same object. Otherwise, return false.
- If x is null and y is undefined, return true.
- If x is undefined and y is null, return true.
- If Type(x) is Number and Type(y) is String,
return the result of the comparison x == ToNumber(y). - If Type(x) is String and Type(y) is Number,
return the result of the comparison ToNumber(x) == y. - If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
- If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
- If Type(x) is either String or Number and Type(y) is
Object,
return the result of the comparison x == ToPrimitive(y). - If Type(x) is Object and Type(y) is either String or
Number,
return the result of the comparison ToPrimitive(x) == y. - Return false.
from notebook.
Related Issues (20)
- react 父子之间组件通信的意外发现
- 设定自己的自动化测试环境
- async2.6.1源码分析之waterfall HOT 2
- async2.6.1源码分析之parallel HOT 1
- async2.6.1源码分析之asyncify
- async2.6.1源码分析之auto
- redux 源码解读
- throttle vs debounce HOT 2
- transducer in javascript HOT 2
- http/2 - High Performance Browser Networking
- http/2 入门
- 二进制浮点数的存储解读
- 尾递归(TCO)
- $elemMatch vs. Dot Notation
- 抽象语法树例子
- three 流光效果 HOT 1
- three 相关资料 HOT 2
- react-intl 单复数转换 HOT 2
- web3 HOT 2
- react
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from notebook.