over 5 years ago

JavaScript __proto___ and Function.prototype

Originally, I thought there is only one prototype property per function until I read about __proto__. Take a look here:

I was totally confused until I read this stackoverflow post. First of all, this graph helps to clarify things a bit:

The accept answer nails it:

__proto__ is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build __proto__ when you create an object with new:

As one of the users commented:

Ah! So prototype is not available on the instances themselves, but only on the constructor functions.

Here is a table to summarize the idea, considering the code blow:

__proto__ prototype
obj Object undefined
Person() Function Object {
constructor: function Person()
__proto__: Object
}
p1 Object {
constructor: function Person()
__proto__: Object
}
undefined

Note: obj.__proto__ is the Object.prototype and Person._proto__ is Function.prototype

over 5 years ago

JavaScript the value of this inside a function

The value of this depends on how the function is called. There are four ways to call a function:

• function invocation
• method invocation
• constructor invocation
• apply/call invocation

Function Invocation

In this case, this inside the function add is always bound to the global variable of the environment. (It is the Window object in this case because I run the code inside a browser)

A special case of function invocation is closure:

It is tempting to think the this value inside the plus() function is tied to add. However, that is not the case as it is explained in this post. plus is still triggered via function invocation, so this still points to the global variable.

Method Invocation

When ever the dot notation is used, a method invocation happens. In this case, the value of this equals to the object before the dot. So in this case, this == person. Because person.name = "John", this.name == "John".

A special case is nested objects:

whoever is closest to the function call is the value of this.

Constructor Invocation

In this case, a new object has been created. The keyword this points to the newly created object. The function setName() is equivelent to the following code:

Apply/Call Invocation

By using Apply or Call (or bind), you can specify the value of this:

When setName() is called the first time, name is created on the window object. If we want to attach the name property to a different object, we can use apply or call (or bind).

over 5 years ago

JavaScript bind, call and apply

Found a really good example that explains the differences among bind, call and apply.

Both call and apply attaches this into function and executes the function immediately:

The difference is that apply can take in an array as the parameters but call has to list them individually:

bind only attaches this with a function but id does not execute the function:

You can also pass parameters to bind:

bind is often used to express the intention of "this function is invoked with a particular this".

over 5 years ago

JavaScript the value of this inside closure

The this keyword inside the inner() function also refers to the Window object. This is because:

When a function is invoked as a function rather than as a method, the this keyword refers to the global object. Confusingly, this is true even when a nested function is invoked (as a function) within a containing method that was invoked as a method: the this keyword has one value in the containing function but (counterintuitively) refers to the global object within the body of the nested function.

over 5 years ago

JavaScript configurable

To understand what configurable does, I did a little experiement:

I wonder if I can change those properties to true if they started as false:

Conclusion

When configurable is set fo false:

• you cannot delete the property by using the keyword delete
• you cannot change the property enumerable
• you CAN change the property writable from true to false but not the other way around
• you cannot change configurable to true if its current state is false

over 5 years ago

JavaScript defineProperty

I did a little experiment just want to see what's the difference between defining object properties "normally" vs defining them using defineProperty:

Here is the output:

Conclusion

When using defineProperty, you have to define all of the properties by yourself, otherwise they are default to false. But if the properties are define normally, then configurable, enumerable and writable are all default to true.

over 5 years ago

Django SMTP AUTH extension not supported by server

I had a local postfix mail server setup and trying to send an email with the following setting:

Here is the code responsible for sending the email:

Because the mail server is setup locally, there is no need to supply any user name or password. So I didn't bother to specify them in the settings.py. When I had the server running, here is the error that I encountered:

The error message is very misleading, but this particular caught my eyes:

I didn't specify any username and password because I don't think I need them. Maybe I should put them back in? So I added the following lines:

Wholah, it worked! But why? So I looked under the hood:

When using the EmailBackend class, a username and password can be passed in as parameters. I didn't pass either of them in my view function. Thus, Django defaults them to settings.EMAIL_HOST_USER and settings.EMAIL_HOST_PASSWORD. However, EMAIL_HOST_USER and EMAIL_HOST_PASSWORD are not defined in the settings file. So by the time self.connection.login(self.username, self.password) is called, both username and password are None.

The error message SMTP AUTH extension not supported by server is generated by python's own smtp library. I guess Django can throw a more meaningful error before this to make this error more apparent.

over 5 years ago

Django template HTML character escape

When django template renders parameters passed from views, it auto escapes characters that are considered as HTML entities.In English, that means characters like the double quote is convereted to &quot;. This default behavior may not be what you want so it is important to know how to turn it off.

Here is a situation that I encountered while working on a project. I need to pull all of the staffusers out of the database and then put their email addresses in the following format:

In the view function, I pull the email addresses out of the database and converted them into JSON format:

Then, in the template:

When I tested the page, this is what I see:

This happened because when python converts the email_list to json, it added double quotes around the key and value pairs. When Django template renders it, it auto escaped the double quote character (meaning convert it to #quot;). To stop Django from doing that:

The autoescape block controls whether the content inside of it should be auto escaped or not. So use it when you dont want to see those HTML entities.

over 5 years ago

The netstat command line arguments

The common command line arguments that I use with the netstat command:

-t: display tcp connections
-u: display udp connections
-l: display ports that are in LISTEN state
-p: display process id
-n: display ip addresses instead of host names

To see if a port has been occupied:

over 5 years ago

Postfix master.lock permission denied

I was trying to start a Postfix mail server on a Ubuntu 12.4:

What? I just started the service and it should be running on port 25 but telnet won't connect, so I had to dig more:

Nothing?! After some Google search, I located the log file at /var/log/mail.err:

No Permission? Ok, let's give it the permission:

Great, it worked!

P.S. chown postfix.postfix = chown postfix:postfix = chown owner_name:group_name