Table of contents
Background
Some time back I had started a Froiz (front-end quiz)
on various topics and received a great response from the community. However unfortunately I wasn't able to find time to continue with the quizzes but finally now I am trying to get back and this blog is an attempt to restart the quiz π₯³
It is recommended to read the blog Hacking Javascript Objects - I before taking the Quiz.
Quiz
If you have not taken the quiz yet you can try it out below or directly go to the link for better experience here
All the best π
Woohoo congratulations on completing the Quiz π! How did it go ?
If you scored 100% then its awesome π! If not, nothing to worry the below section is definitely going to help so let's dive into the Solutions of the quiz.
Solutions
An attempt to "Hack"
console.log
π±Object.defineProperty(console, "log", { value: () => "console.log is hacked π±", }); console.log("Hello world!");
Explanation
The output will beππ»
console.log is hacked π±
Overriding the behaviour of
console.log
is possible, as thelog
property in theconsole
object iswriteable
.Object.getOwnPropertyDescriptor(console, 'log').writable // true
What will be the value of
arr
and why π€ ?var arr = [1, 2, 3, 4, 5]; arr.length = 2; console.log(arr);
Explanation
Arrays
are list-likeobjects
, andlength
property iswriteable
hence it reduces the length of the array when updatinglength
. Hence the value ofarr
will be ππ»[1,2]
If you increase the
length
then it will addempty slots
to the remainingindexes
var arr = [1, 2, 3, 4, 5]; arr.length = 7; arr // [1, 2, 3, 4, 5, empty Γ 2]
Note: The behavior need not be the same in all browsers as it depends if the browsers permit redefining the length of the array. Also do not use this in production π
You can always check if the browser allows it
var arr = [1, 2, 3, 4, 5]; Object.getOwnPropertyDescriptor(arr, 'length') // {value: 7, writable: true, enumerable: false, configurable: false} in chrome
This means you can
update
the length attribute but cannotdelete
orenumerate
.Pass
orFail
? π€π»var myObj = { marks: 60 }; myObj = Object.defineProperty(myObj, "result", { get: () => { if (myObj.marks < 50) return "Fail"; else return "Pass"; }, }); console.log(myObj.result); myObj.result = "modified result"; console.log(myObj.result);
Explanation
The output will be ππ»
Pass Pass
Since
myObj.marks
is60
hence when accessing theresult
attribute , theget
method forresult
will return "Pass" asmyObj.marks < 50
evaluates to false πget: () => { if (myObj.marks < 50) return "Fail"; else return "Pass"; }, }); console.log(myObj.result); // will print "Pass"
As
myObj.result
doesn't have aset
method so setting the value usingassignment (=) operator
is not possible. HencemyObj.result
will still log "Pass".// This will not work as `set` method is not present myObj.result = "modified result"; console.log(myObj.result); // will print "Pass"
Additionally even if
set
method is added still when accessing the value returned will bePass
orFail
as per theget
method's implementation π.Playing with
console
π. What willconsole.happy()
evaluate to?Explanation
The output will be ππ»
"π" "π"
Line
2
of course evaluates to "π" as thats what thevalue
is being set to forhappy
property.What happens when the line 3 is executed ?
delete console.happy;
Since the property
happy
property ofconsole
object is notconfigurable
hence it cannot bedeleted
.Object.getOwnPropertyDescriptor(console, 'happy').configurable // false
console.happy(); // "π"
As property
happy
couldn't be deleted hence line 4 evaluates to "π".Trying out with Functions π§
function Dev() { Object.defineProperties(this, { name: { set: (name) => { this.devName = name; }, get: () => { return this.devName; }, }, type: { set: (devType) => { type = devType; }, get: () => { return type; }, }, }); } dev1 = new Dev(); dev1.name = "Maria"; dev1.type = "backend"; dev2 = new Dev(); dev2.name = "June"; dev2.type = "frontend"; console.log(`${dev1.name} is a ${dev1.type} developer and ${dev2.name} is a ${dev2.type} developer`);
Explanation
Functions
areobjects
as anymethod
orproperty
can be attached to the function just like regularobjects
.The
set
andget
methods are defined for both thename
andtype
properties of the function.However, there is a slight difference, for
name
the property points to the object which is accessing the property hence every object has its own name, whereas fortype
the property is shared by all objects.So every developer has its own name but since the
type
shared by all objects so it will be same for all devs. Thetype
will befrontend
as thats what is set at last and shared by all.dev1 = new Dev(); dev1.name = "Maria"; dev1.type = "backend"; dev2 = new Dev(); dev2.name = "June"; dev2.type = "frontend"; // shared by all devs
Hence the output will be ππ»
Maria is a frontend developer and June is a frontend developer
Closing thoughts
Are you still there ? Thank so much for reading! Hope you enjoyed the Quiz and learned something π. Will be coming up with more content, quiz and sharing experiences soon so stay tuned!