GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — JavaScript-Design-Patterns/GoF/idiomatic/Creational/Prototype at master · irynaO/JavaScript-Design-Patterns

Expand file tree

/

Prototype

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

Prototype

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
CoffeeScript
CoffeeScript
 
 
ECMAScript
ECMAScript
 
 
TypeScript
TypeScript
 
 
Prototype.dia
Prototype.dia
 
 
Prototype.png
Prototype.png
 
 
README.md
README.md
 
 

README.md

Outline

Synopsis

When I order something, I sometimes like to have a paper invoice. But when the order is important, I do not want to lose the document. Therefore, I want to be able to make photocopies.

Problem

Creating a new object with "new" is not the same thing as cloning an existing object. The good point about JavaScript is that the language is prototype-based. The built-in Object.create() method already performs some kind of cloning. However, we cannot clone properly if we only use Object.create(). The parameter of this method (the original object) is used as the prototype of the new object. This means the new object will not have own properties like the original object. These properties would be available in the new object, but in its prototype.

Solution

We need only two things in this situation:

  • An object (literal) that we want to clone (e.g. invoice)
  • A custom cloning function that will take the object to clone as a parameter

Prototype (idiomatic)

N.B. Most of the time, it is not even useful to have an exact copy of an object in JavaScript. The important thing is often to be able to use the same properties on several objects, and for that purpose, a simple Object.create() is generally good enough.

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.