การสร้างเมธอดส่วนตัวอย่างแท้จริงใน Javascript ทำให้แต่ละอ็อบเจ็กต์มีสำเนาของฟังก์ชันของตัวเอง สำเนาเหล่านี้ไม่เก็บขยะจนกว่าวัตถุจะถูกทำลาย
ตัวอย่าง
var Student = function (name, marks) {
this.name = name || ""; //Public attribute default value is null
this.marks = marks || 300; //Public attribute default value is null
// Private method
var increaseMarks = function () {
this.marks = this.marks + 10;
};
// Public method(added to this)
this.dispalyIncreasedMarks = function() {
increaseMarks();
console.log(this.marks);
};
};
// Create Student class object. creates a copy of privateMethod
var student1 = new Student("Ayush", 294);
// Create Student class object. creates a copy of privateMethod
var student2 = new Student("Anak", 411);