var add = function() { /* even though we forgo specifying parameters when defining the function, we can rely on the arguments array passed to the function to access parameters */ return arguments[0] + arguments[1]; }; console.log(add(4, 4)); // returns 8
Saturday, May 14, 2016
arguments
Friday, May 13, 2016
吐槽温哥华房产交易
温哥华买房,有几点对买家不利:
- 竞价机制 一般来说,卖方会出一个比政府估价高很多的价格,在这个价格之上,各买家出价,第一次竞价已经把房价炒到很高了,但是买家还可以加价,这不尼玛扯吗,怪不得房价这么高。
- 障碍解除机制 除了在出价上PK之外,买方的offer(购买意愿书)还包括一个障碍清单,这本来是利好买家,可是如果你出的是带条件的offer,卖方就基本不会接受。原本先天属于买家的这种权利就这样被剥夺了!
- 验房成为一种奢侈 验房是买家的先天权利,在无条件offer中被剥夺。
- 贷款成为巨大风险 买家在买房的时候不可能知道自己能不能贷到款,而障碍解除机制中你不能把贷不到款作为你的障碍,这样你的定金就可能打水漂。
- 评估成为一种猫腻 因为在竞价中把房价炒到很高,所以银行很可能不愿意按照那个价格给你贷款,这时候差价你就得自己去凑,凑不到的话定金还是打水漂。买家同样不能把评估风险作为障碍。
Wednesday, May 11, 2016
primitive_and_prototype_chain
var i = 10 Object.prototype.foo = 'Foo'; alert( i.foo ) //Foo i.bar = 'Bar' // No effect: primitive is not extensive in own props alert( i.bar )// undefined alert( i.constructor ) // function Number(){} alert( typeof i.constructor.prototype ) //object
Sunday, May 8, 2016
reference
var myObject = {};
var copyOfMyObject = myObject;
myObject.foo = 'bar';
console.log(myObject, copyOfMyObject); // logs 'Object { foo="bar"} Object { foo="bar"}'
var copyOfMyObject = myObject;
myObject.foo = 'bar';
console.log(myObject, copyOfMyObject); // logs 'Object { foo="bar"} Object { foo="bar"}'
=== operator for js
var price1 = 10; var price2 = 10; var price3 = new Number('10'); var price4 = price3; var price5 = new Number('10'); console.log(price1 === price2); // logs true: primitive values compared by value console.log(price1 === price3); //logs false: compares object with primitive console.log(price4 === price3);// logs true: same references alert("now checking price5...") console.log(price5 === price3);// logs false: diff references
Monday, January 18, 2016
single-threaded apartment
/*Rules for single-threaded apartments are simple, but it is important to follow them carefully:
Every object should live on only one thread (within a single-threaded apartment).
Marshal all pointers to objects when passing them between apartments./*传统线程可以访问同进程内的资源*/
Each single-threaded apartment must have a message loop to handle calls from other processes and apartments within the same process. Single-threaded apartments without objects (client only) also need a message loop to dispatch broadcast sendmessages that some applications use.
DLL-based or in-process objects do not call the COM initialization ;Each thread of a client process or out-of-process server must call CoInitialize, or call CoInitializeEx and specify COINIT_APARTMENTTHREADED for the dwCoInit parameter。The main apartment is the thread that calls CoInitializeEx first.
DLL-based or in-process objects register their threading model with the ThreadingModel named-value under the InprocServer32 key in the registry. Apartment-aware objects must also write DLL entry points carefully. */
COM creates a hidden window using the Windows class "OleMainThreadWndClass" in each single-threaded apartment. A call to an object is received as a window message to this hidden window. When the object's apartment retrieves and dispatches the message, the hidden window will receive it. The window procedure will then call the corresponding interface method of the object.
Single-threaded apartments can implement IMessageFilter 。
综上,单线程套间本质上是一个类(接口),保存着一个窗口句柄、一个线程(线程先初始化com库[就是注册],然后有消息loop)、0或多个组件对象、客户线程(也需要在com注册),该类(接口)可以进一步实施IMessageFilter接口。
In-Process Server Threading Issues
Like other servers, in-process servers can be single-threaded, apartment-threaded, or free-threaded. These servers can be used by any OLE client, regardless of the threading model used by that client.
For an in-process server, when the threading model of the client and in-process server differ, COM must interpose itself between the client and the object. When an in-process object that supports the single-threaded model is called simultaneously by multiple threads of a client, COM cannot allow the client threads to directly access the object's interface—the object was not designed for such access.Instead, COM must ensure that calls are synchronized and are made only by the client thread that created the object. Therefore, COM creates the object in the client's main apartment and requires all the other client apartments to access the object by using proxies.
When a free-threaded apartment (multithreaded apartment model) in a client creates an apartment-threaded in-process server, COM spins up a single-threaded apartment model "host" thread in the client. This host thread will create the object, and the interface pointer will be marshaled back to the client's free-threaded apartment. Similarly, when a single-threaded apartment in an apartment-model client creates a free-threaded in-process server, COM spins up a free-threaded host thread (multithreaded apartment on which the object will be created and then marshaled back to the client single-threaded apartment).
COM helps protect access to objects provided by a single-threaded DLL by requiring access from the same client apartment in which they were created. In addition, all of the DLL entry points (like DllGetClassObject and DllCanUnloadNow) and global data should always be accessed by the same apartment. COM creates such objects in the main apartment of the client, giving the main apartment direct access to the object's pointers. Calls from the other apartments use interthread marshaling to go from the proxy to the stub in the main apartment and then to the object. This allows COM to synchronize calls to the object. Interthread calls are slow, so it is recommended that these servers be rewritten to support multiple apartments.
single-threaded的全称应该是single-thread-per-process。
Subscribe to:
Posts (Atom)