Javascript / Jquery — Prototype Pattern — StudySection Blog

Javascript / Jquery — Prototype Pattern — StudySection Blog

What is a Prototype Pattern?

The prototype pattern creates objects by cloning an existing object, rather than creating non-initialized objects. It returns objects that are initialized with values that it copied from an object. This is also referred to as the “properties” pattern.

Prototype patterns are based on prototypal inheritance, in which we create objects that act as prototypes for other objects. Each object that the constructor builds is based on the prototype object. If the prototype of the constructor contains a property called “name,” for example, then each object created by that same constructor will also have this same property.

Examples of the prototype pattern in Javascript / Jquery

Example 1:
function person(data) {
this.data = data;
this.name = $(data).find('name');
this.designation = $(data).find('designation');
this.experience = $(data).find('experience');
};
person.prototype.init = function() {
$(this.elem).append($('<option value=""> ----- Select a Name ----- </option>'));
var t = this;
$(this.name).each(function() {
$(t.elem).append($('<option value="' + $(this).attr('name') + '">' +$(this).attr('name')+'('+ $(this).attr('designation')+ ')</option>'));
});
$(this.elem).combobox(); // from jQuery UI combobox extension
};

Example 2:
function Person(first, last, age, eye) {
this.firstName = first;
this.lastName = last;
this.age = age;
this.nationality = "English";
}
const myFather = new Person("John", "Mehta", 50, "blue");
const myMother = new Person("Sally", "Kaur", 48, "green");
document.getElementById("details").innerHTML =
"My father's nationality is " + myFather.nationality + " and the nationality of my mother is " + myMother.nationality;

People having good knowledge of Financial accounting can get an Accounting Certification Exam from StudySection to increase their chances of getting a job in this field. You can get a foundation level certification if you are new to Financial accounting or you can go for advanced level certification if you have expert level skills in Financial accounting.

Originally published at https://studysection.com on January 12, 2022.

--

--

The most loved online platform for eCertification in several subjects including but not limited to Software Development, Aptitude and more.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Study Section

Study Section

The most loved online platform for eCertification in several subjects including but not limited to Software Development, Aptitude and more.