JavaScript do not have function overloading because functions are objects, this is something we can alternatively do about it
發表留言Object Mutability in Python
Internal state
changing the data inside the object is called modifying the internal state of the object, the state(data) is changed, but memory address has not changed
Mutable
an object whose internal state can be changed
- Lists, Sets, Dictionarys, User-defined Classes
Immutable
an object whose internal state can not be changed
- Numbers(int, float, Booleans), String, Tuples, Frozen Sets, User-defined Classes
- variable re-assignment change the reference not the internal value
發表留言
Keyword “Arguments” and Spread
“arguments” is another keyword JavaScript engine create during the creation of execution context, arguments contains a list of all the values of all the parameters that passed to a function.
argument is an array-like object, it doesn’t has all the features of JavaScript array
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function MyFunc(arg1, arg2, arg3){ console.log(arguments); // [] console.log(typeof(arguments)); // Object if(arguments.length === 0){ return; } console.log(arguments[0]); } MyFunc(); |
“arguments” will become deprecated in the future, the new thing is called spread
1 2 3 |
function myFunc(arg1, arg2, arg3, ...other){ // avaliable in the future... } |
發表留言
Variables in Python
variables are memory references, not equal to the object but reference(alias) the object at memory space.
Find out memory address referenced: using id()
reference counting
after we created a object in memory, python keep track of the number of the references we have to the object, as soon as the count goes to 0, python memory manager destroy object and reclaim the memory space
Find out reference count:
- using sys.getrefcount()
- using ctypes.c_long.from_address()
circular references
in the circumstance of circular reference, the reference count could not goes to 0(memory leak), need garbage collector to identify it
garbage collection
can be control programmatically using the gc module, turned on by default, beware to turn it off, for python < 3.4, if even one of the objects in the circular reference has a destructor, the destruction order may be important, but the GC does not know what order should be, so the circular reference objects will be marked as uncollectable and cause memory leak
dynamically typing
python variable name has no references to any type, when we use type(), python looks up the object which is referenced and return the type of the object
variable equality
- identity operator(var_a is var_b) compare the memory address
- equality operator(var_a == var_b) compare the object state
Keyword “this”
JavaScript engine every time an execution context is created, that is every time a function is RUN, it gives a variable called “this” pointed to a different object depends on how the function is invoked(where the function is and how it been called)
1 2 3 4 5 6 7 8 9 10 11 |
function greeting(){ console.log(this) } greeting(); // Window var man = { greeting: function(){ console.log(this); } } man.greeting(); // Object { greeting: greeting() } |
Weird part
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
var man = { greeting: function(){ function someFunctionSitInObjectFunction(){ console.log(this); } someFunctionSitInObjectFunction(); } } man.greeting(); // Window /* ==== Solution ==== */ var man = { greeting: function(){ var self = this; // by references function someFunctionSitInObjectFunction(){ console.log(self); // look for self variable in scope chain } someFunctionSitInObjectFunction(); } } man.greeting(); // Object { greeting: greeting() } |
發表留言
By Value v.s. By Reference
by value (primitive type)
copying the value into two separate spots in memory
by reference (all objects)
no copy of object is created, instead, two names or more point to same address
1 2 3 4 5 6 7 8 9 |
var a = { greeting: 'hello', }; var b = a; a.greeting = 'hola'; b.greeting === 'hola'; //true /* set up a new memory space and point b to if */ b = { greeting: '你好' }; a.greeting != b.greeting; //true |