Found a great SO post on this topic:
[[Construct]] property for a
F is called, the following steps are taken:
- Create a new native ECMAScript object.
- Set the
- Get the value of the prototype property of
Result(3)is an object, set the
Result(3)is not an object, set the
Result(1)to the orig
inalObject` prototype object as described in 184.108.40.206.
- Invoke the
[[Call]]property of F, providing
Result(1)as the this value and providing the argument list passed into
[[Construct]]as the argument values.
Type(Result(6))is Object then return
Let me translate that into code:
You can add an
return statement in the constructor to hijack what it returns:
If the returned object is not an object, then step 8 kicks in:
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.
prototypeis the object that is used to build
__proto__when you create an object with
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:
constructor: function Person()
constructor: function Person()
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
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.
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
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:
bind), you can specify the value of
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).
Found a really good example that explains the differences among
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 is often used to express the intention of "this function is invoked with a particular this".
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.
I defined two properties (one enumerable and one not enumerable) on a prototype object and two properties on an instance object. I want to see what is the difference among
Here is a summary of which method displays which properties:
|Methods||Prototype Object||Instance Object|
|Enumerable||Not Enumerable||Enumerable||Not Enumerable|
To understand what configurable does, I did a little experiement:
I wonder if I can change those properties to
true if they started as
When configurable is set fo
- you cannot
deletethe property by using the keyword
- you cannot change the property
- you CAN change the property
true to falsebut not the other way around
- you cannot change
trueif its current state is
I did a little experiment just want to see what's the difference between defining object properties "normally" vs defining them using
Here is the output:
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
writable are all default to
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
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.
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
". 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: