每天积累一丢丢(Wed, 4 May 2016)

来源:互联网 发布:分体式马桶 知乎 编辑:程序博客网 时间:2024/04/30 12:28

prototype vs. __proto__

prototype is a property of a FUNCTION OBJECT.   __proto___ is internal property of an object(), pointing to its prototype (try Object.getPrototypeOf() here:

<span style="font-size:18px;">var a = new Object();Object.getPrototypeOf(a) === a.__proto__; // true!</span>
but a.__proto__ is quicker, but this .getPrototypeOf(a) gives a more intuitive name.

Also, as everyone can predict,

<span style="font-size:18px;">var a = new Object();Object.prototype === a.__proto__; // true</span>

A very good way to remember this is to think:

http://stackoverflow.com/questions/9959727/proto-vs-prototype-in-javascript

Prototype is used when creating a constructor function but foo.prototype.sth shouldn't be used. Think __proto__s are 'installed prototype' and can be used like foo.sth (after new Foo)

And the constructor can be still found inside __proto__ by:

<span style="font-size:18px;">a.__proto__.constructor === Object;</span>
<span style="font-size:18px;"></span>

new Object() vs. {}

This two methods of creating objects make me confused, when I tried codes below:

<span style="font-size:18px;">var a = new Object();var b = {};a === b; // false</span>
http://stackoverflow.com/questions/4597926/what-is-the-difference-between-new-object-and-object-literal-notation

According to this article, these two methods are the same and the second one is preferred. Besides, using {} won't invoke the Object() constructor.

But why a===b gave false,UNDER CONSTRUCTION

http://stackoverflow.com/questions/1068834/object-comparison-in-javascript

This is not an issue of how objects are created but incorrectly used '==='

Besides, http://blog.csdn.net/sinat_16073327/article/details/44411697

function New (f) {    var n = { '__proto__': f.prototype };    return function () {        f.apply(n, arguments);        return n;    };}
Gave a very good explanation on what New does.


What instanceOf() does?

This problem has been bothering me now and then, it 'tests whether an object has in its prototype chain the prototype property of a constructor'

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof

Like a riddle... To make things simpler, instanceOf() help to check if this object is a instance of the constructor.


Basic concept: expression context and statement context

Expression context means contexts which can return a value, like:

<span style="font-size:18px;">var a = {  name: 'haha'}</span>
In a expression context, '{' and ‘}’ helps to tell the expression starts and ends.

Statement context means contexts which tell the start and end of a statement block:

<span style="font-size:18px;">var haha = 'haha';if (haha) {}</span>

递归函数的解耦:

注意arguments.callee 和 caller都不可在'use strict'下使用

<span style="font-size:18px;">// function factorial(num) {//   if (num <= 1) {//     return;//   } else {//     return num * factorial(num-1);//   }// }// Tofunction factorial(num) {  if (num <= 1) {    return;  } else {    return num * arguments.callee(num - 1);  }}</span><span style="font-size: 24px;"></span>
另外由于 function functionName() {} 中的functionName其实只是个指针,指向这个function,所以有
<span style="font-size:18px;">var secondFact = factorial;factorial = function() {  return 'Nothing here'}console.log(secondFact(3)); // 6console.log(factorial(3)); // 'Nothing here'</span>

Properties and variables: 

<span style="font-size:18px;">function foo() {  a = 1;  var b = 2;  console.log(window.a); // 1  console.log(foo.b); // undefined}</span>
Why cannot we get b 's value by foo.b? 

https://javascriptweblog.wordpress.com/2010/08/09/variables-vs-properties-in-javascript/

Seems like this issue has something to do with global and function context. 'In global context, the global object(parent of properties) and the global variableObject(parent of variables) happen to be the same'


Why there exists Function declaration hoisting:

I can:

<span style="font-size:18px;">foo();function foo() {};// While I cannotfoo();var foo = function() {};</span>
This is due to the 'function declaration hoisting' but why is there a such things? 
http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/
This article gave a very clearly explanation: Any function will be called (entering creation stage) and executed(entering execution stage), 

in the first stage: 

Scope Chain will be created; variables, functions and arguments will be generated; This value will be determined.

As for the second stage:

Assign values, references to functions and interpret/execute code (?);

From information above, the function foo() {} is declared since it is created in the first stage, no wonder why it can be invoked correctly. In the second case, we created a variable 'foo' firstly, however, the value (is a function here) won't be assigned until it enters the second stage thus foo() cannot be found.

One thing needs to be noted: 

<span style="font-size:18px;">function() {    console.log(typeof foo);     function foo() {        return 'hello';    }    var foo = 'hello';}());</span>

In this case, seems like the declaration of var foo will overwrite foo() the function pointer but i WILL NOT, in the creation stage if the variable name has already existed in the variable object (and foo is activation object), this declaration will be bypassed. 

And for information about activation object: 

http://programmers.stackexchange.com/questions/189967/what-is-an-activation-object-in-javascript


IIFE:
http://www.cnblogs.com/zichi/p/4401755.html


'Use strict':

http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html

与我有关的:

1. function里的this不会再指向全局, 而是直接报错说 'undefined'

2. 属性无法被删除除非设置为configurable: true

3. 如果是一个writable为false的property, 改写会报错

4. 对利用getter的属性进行赋值,会报错

5. 尝试删除不可删除的属性,会报错 (delete Object.prototype)

6. 对象内多个属性重命名, 会报错(非use strick会覆盖)

7. 参数无法同名

8. 函数声明必须在顶层(不能嵌套在if, for and while etc 中)


















0 0
原创粉丝点击