Friday, 25 August 2017

Why You Must Reset The Constructor!


In my last write up, we realized the beauty of the constructor property of an object. We discussed how easy the said property made it for us to create an object of the same type without having direct access to the function that creates the objects.

So, where does the constructor property come from anyway!

When you access oStudent.constructor, you get the access to the constructor function of oStudent. However, oStudent does not contain the constructor property. Take a look at the snapshot below:



As can be seen in the watch snapshot above, the object oStudent does not contain any property in the name of constructor. So where does it get this property from? Recall my post on prototypes, and you will realize that the property comes from the prototype of its constructor function. Assume that the constructor function of oStudent is Student, and so lets watch prototype of Student below:


Voila! there it is. The property constructor comes from the prototype of the constructor function (Student) that created our object oStudent. Now, we know where the constructor property comes from, when you do oStudent.constructor.

So, what does this have to do with constructor resetting?

Again if you go back to my discussion on prototypes, you will know that you could augment (add) properties to the prototype of a function. You could add any number of properties. However, at times you may also want to replace the prototype itself, with your own object. In that case, you will have replaced the default prototype of the function with your own function, as below:


The trouble with doing this is, you lose the constructor property that the default prototype had. See what our new prototype contains vs what the original prototype contained:


On the left we see that the original prototype contains the constrictor property, but on the right the new prototype that we set does not have it. So therefore any new objects created from the function Student will not get the constructor property. So you will not be able to make use of the constructor property of the object if you wanted to.

How do I ensure the constructor property is not lost?

When you replace the default prototype with your own object, make sure that your new prototype keeps the constructor property. Below is how you would do it:


Simple, right! but extremely useful. Look below, how our new prototype looks now:


There you go! Our prototype now has the necessary property 'constructor'. Now, all new instances of the function Student will have access to it.

Can you now guess the output of lines 16 and 25 below?


Give it a shot and find out!


41 comments:

  1. Line 16 gives function Student (){} and line 25 gives function Object() {} , because when ever we overwrites the prototype its default constructor property is removed and hence the constructor property accessed later is of Object superclass through prototype

    ReplyDelete
  2. If you are interested in a career in the construction industry then it is important that you know your work well. For any success in construction work it is mandatory that you receive the required training and are able to handle the kind of projects that are assigned to you. شرکت آرین سازه

    ReplyDelete
  3. Auto maintenance and repair can be costly, and few people look forward to a trip to their local auto garage for routine maintenance, let alone emergency repairs. Major auto repair jobs can be expensive as well as time consuming but can often be prevented by routine maintenance and by knowing a few basics about your vehicle. Stainless Braided Hose

    ReplyDelete
  4. The 'Great Recession' theoretically lasted about 18 months, from 2007 to 2009. Recovery has been agonizingly slow in many industries but we are now in 2015 and the bathroom restore industry is more rapidly shrugging off the residual effects of the recession.

    ReplyDelete
  5. If you are interested in a career in the construction industry then it is important that you know your work well. For any success in construction work it is mandatory that you receive the required training and are able to handle the kind of projects that are assigned to you. Tour in Saudi Arabia

    ReplyDelete
  6. Commercial รับทาสีอาคาร are places of business. Value creation is done every day in every space. When the facility is a venue of trade and industry, what role do commercial painting contractors play to support the business?

    ReplyDelete
  7. Kudos on your wonderful posting… We certainly liked reading this, you might be a terrific source I will always take a note of ones weblog and definitely will come back down the road I want to motivate yourself to continue this good posts, enjoy your weekend? OMG how about Gaddafi notable news! Thx ! Online Website Builder steroids for sale

    ReplyDelete
  8. When choosing a bed, the mattress should be chosen separately from a specialist firm. The bed should be selected to suit the general decor of your room and/or its existing furniture, while its comfort is determined by the design of the mattress. There are many possible mattress designs consisting of various layers, each of which has a part to play in your comfort. Best beds

    ReplyDelete
  9. It has always been a debate whether to buy new or used construction equipment. Smaller fleets prefer to buy used construction equipment as they attract less capital investments. Another reason for people to opt for used construction equipment is that they are sometimes as good as new and come at a very heavy discounted price as compared to that offered at the showrooms. Towing Company Lake Charles LA

    ReplyDelete
  10. It has turn out to be very much common for people to ignore their day-to-day plumbing issues. Generally, we apt to brush our plumbing related issues under the carpet - in the event that they can do so. They postpone the servicing of their hot water systems or heating systems with a belief that nothing could possibly go wrong. commercial plumber new orleans

    ReplyDelete
  11. It has turn out to be very much common for people to ignore their day-to-day plumbing issues. Generally, we apt to brush our plumbing related issues under the carpet - in the event that they can do so. They postpone the servicing of their hot water systems or heating systems with a belief that nothing could possibly go wrong. plumbers ogden utah

    ReplyDelete
  12. It refer to the various kinds of tools and equipments used for building and construction purposes. It has always been an integral part of the construction and building industry as it is unimaginable to construct a building without these tools. In fact, for every single activity at a construction site one invariably needs one or the other kind of construction tool to start up the work. paving contractors New Caney

    ReplyDelete
  13. The winter's harsh weather may have damaged your home plumbing without you being aware. Here are some recommendations from residential plumbers regarding how to carry out a home plumbing inspection this spring. salt lake plumber

    ReplyDelete
  14. Plumbing issues are some of the more usual types of problems that you might encounter in your home. There are different types of plumbing concerns that can come up. You have to keep yourself informed about these issues that concern plumbing, so that you can handle these on your own or at least you would have an idea of how they can be cleared. budget plumbing utah

    ReplyDelete
  15. Plumbing services are important to make homes better and safer. This is possible since plumbing services can help you install pipes, valves and fitting properly. Provo Plumber Utah

    ReplyDelete
  16. The winter's harsh weather may have damaged your home plumbing without you being aware. Here are some recommendations from residential plumbers regarding how to carry out a home plumbing inspection this spring. Plumbers Layton Utah

    ReplyDelete
  17. If you are interested in a career in the construction industry then it is important that you know your work well. For any success in construction work it is mandatory that you receive the required training and are able to handle the kind of projects that are assigned to you. concrete repair houston

    ReplyDelete
  18. At Tampa Bay Pavers in Tampa, Fl. we set your vision in stone. We offer the highest quality paving products and services at the most affordable price. We specialize in custom designs and expert installation of paver driveways, patios, pool decks, walkways, summer kitchens, fire pits and much more. Come see why we are the fastest growing pavers in Tampa Florida Pavers in Tampa

    ReplyDelete
  19. These article highlights three key areas of safety in construction. These three areas are (i) procedures and processes (ii) work wear and (iii) safety equipment. remodeling San Jose CA

    ReplyDelete
  20. Many participants in the construction industry are affected by construction law issues to include subcontractors, builders, homeowners, contractor, business owners, and more. This area of law involves the application of several general bodies of law to industry problems. It also includes issues that result from the association of the construction process with its business relationships; the laws that govern the construction process; and the legal principles and laws that are in connection with different kinds of professions and businesses that serve the construction industry. interior design

    ReplyDelete
  21. Local Landscaping Cranbourne is your premium local landscaping service in Cranbourne and the surrounding area. Our services include Landscape and Garden Design, Landscape Construction, Landscape Water Features, Landscape Lighting Installation, Landscape Maintenance Service, Gardening Service, Lawn Care Installation and Maintenance Services, including the area around your driveway. No job is too big or too small for your Cranbourne landscaping requirements. Call Local Landscaping Cranbourne today to discuss your requirements on (03) 5910 3000 Cranbourne Landscaping

    ReplyDelete
  22. Marketing has become harder in the construction sector in recent years, due mainly to the economic down turn. This is an article looking at what construction companies can do to survive and what marketing options are open. 24/7 Mobile Repair

    ReplyDelete
  23. Some truly nice stuff on this website , I it. kitchen remodel ann arbor

    ReplyDelete
  24. Thank you for making the honest attempt to discuss this. I feel very strong about it and would like to learn more. If it’s OK, as you gain extra in depth knowledge, might you mind adding more articles very similar to this one with more information? It would be extraordinarily useful and useful for me and my friends. ann arbor remodeling

    ReplyDelete
  25. Marketing has become harder in the construction sector in recent years, due mainly to the economic down turn. This is an article looking at what construction companies can do to survive and what marketing options are open. concrete companies in lansing mi

    ReplyDelete
  26. If you are interested in a career in the construction industry then it is important that you know your work well. For any success in construction work it is mandatory that you receive the required training and are able to handle the kind of projects that are assigned to you. MacDonald Watrobsky Group - Coldwell Banker Lakewood Ranch

    ReplyDelete
  27. It refer to the various kinds of tools and equipments used for building and construction purposes. It has always been an integral part of the construction and building industry as it is unimaginable to construct a building without these tools. In fact, for every single activity at a construction site one invariably needs one or the other kind of construction tool to start up the work. Oasis Realty Group

    ReplyDelete
  28. Some truly nice stuff on this website , I it. handyman lansing mi

    ReplyDelete
  29. Construction in Kansas has achieved a lot of growth owing to the economic development of the state. This however does not mean that there are plentiful jobs in the state. deckbuildersannarbormi.com/

    ReplyDelete
  30. Construction administration involves numerous people, permits, legally binding contracts, competitive bids, and a substantial amount of time and money. Yet there are ways to guard against spiraling construction costs and expanded deadlines for project completion. Handyman

    ReplyDelete
  31. Construction in India is the second most important activity, next only to agriculture. The country is on the path of fast development of infrastructures in the fields like education, health, power and research. handyman ann arbor

    ReplyDelete
  32. If you are interested in a career in the construction industry then it is important that you know your work well. For any success in construction work it is mandatory that you receive the required training and are able to handle the kind of projects that are assigned to you. Visit our site

    ReplyDelete
  33. Construction in India is the second most important activity, next only to agriculture. The country is on the path of fast development of infrastructures in the fields like education, health, power and research. Pool repair pros of Tucson

    ReplyDelete
  34. Construction loan is best suited for availing finance in order to construction home. One of many advantages of construction loan is that the borrower pays only interest till the construction is fully completed. Principal amount can be repaid when home is finally constructed. Moreover you can even turn the loan into a mortgage loan later in case you are unable to pay off the loan. Read the article for different aspects. retirement community orlando florida

    ReplyDelete
  35. Everyday, thousands people all across America make the decision to begin the process of buying a home, or decide to put their current home up for sale. houseplant advisor

    ReplyDelete
  36. Without plumbers a building project could not be completed. A plumber is responsible for installing the entire plumbing system and its components in new buildings. These skilled workers are also very adept at repairing damaged water lines as well as other problems with existing plumbing. To become a qualified plumber an individual will need to complete a training and apprenticeship program from a technical school. Cranbourne Landscaping

    ReplyDelete
  37. It has always been a debate whether to buy new or used construction equipment. Smaller fleets prefer to buy used construction equipment as they attract less capital investments. Another reason for people to opt for used construction equipment is that they are sometimes as good as new and come at a very heavy discounted price as compared to that offered at the showrooms. Roadside Assistance

    ReplyDelete
  38. Construction is a branch of architecture in which form, shape and quality take on a very important role. While designing Construction Logo Designs, a logo designer needs to pay attention to details regarding form, shape, color and proportions. If you carefully examine a good quality construction logo design, you will observe that the designer has taken care of all the essentials such as making the little noticeable and the name-plastered in such a manner as to command the most visibility. Roadside Assistance

    ReplyDelete
  39. If you are interested in a career in the construction industry then it is important that you know your work well. For any success in construction work it is mandatory that you receive the required training and are able to handle the kind of projects that are assigned to you. siding

    ReplyDelete
  40. A construction birthday party is perfect for your little builders. So, whether you're serving up your dirt dessert from a bright yellow dump truck or directing your crew through construction zone activities, put on your hard hat and get ready to construct some birthday fun! Car Towing

    ReplyDelete

The HTML Attributes Never Change!

First things first! What is an HTML attribute? The html attributes provide additional information about or to the element . For instan...