Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Object.create commit #4545

Merged
merged 13 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
85 changes: 85 additions & 0 deletions content/javascript/concepts/objects/terms/create/create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<<<<<<< HEAD
Title: 'Object.create() in JavaScript'

Description: 'object.create() method creates a new object, using an existing object as the prototype of the newly created object.It basically allows you to create a new object that inherits properties and methods from a specified prototype object. It's not about making a copy of an existing object, but rather establishing a prototype chain.'

Subjects:

- 'Web Development'

- 'Computer Science'

- 'javascript'

- 'objects'

- 'prototype'

Tags:

- 'javascript'

- 'objects'

- 'inheritance'

- 'prototype'

- 'Documentation'

CatalogContent:

- 'introduction-to-javascript

- 'paths/front-end-engineer-career-path'
=======
---
mamtawardhani marked this conversation as resolved.
Show resolved Hide resolved
Title: 'Object.create()'
Description: 'It is a static method that creates a new object, using an existing object as the prototype of the newly created object. It allows you to create a new object that inherits properties and methods from a specified prototype object. It's not about making a copy of an existing object, but rather establishing a prototype chain.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
Tags:
- 'Algorithms'
- 'Inheritance'
- 'Objects'
- 'OOP'
CatalogContent:
- 'introduction-to-javascript
- 'paths/front-end-engineer-career-path'
---
>>>>>>> 53b7d41d2838e085538be0eef5c409847d36f782

## Syntax
```js
Object.create(prototype)
Object.create(prototype, propertiesObject)
```
mamtawardhani marked this conversation as resolved.
Show resolved Hide resolved

The `prototype` is the properties newly created object will have from the old object. if the prototype is not specified the newly created object will not inherit any properties or methods from any other object.

The `propertiesObject` parameter is optional and allows you to add additional properties to the newly created object.You can specify which properties you want to add to the new object. For example, you might want to add a property called name, age, or anything else.

## Example
mamtawardhani marked this conversation as resolved.
Show resolved Hide resolved

Lets take a look at an example that explains this concept better
amarachi-nwokocha marked this conversation as resolved.
Show resolved Hide resolved

```js
// Creating an object to serve as the prototype
var prototypeObject = {
greet: function() {
return 'Hello!';
}
};

// Creating a new object with prototypeObject as its prototype
var newObj = Object.create(prototypeObject);

// newObj inherits the greet function from prototypeObject
console.log(newObj.greet()); // Output: "Hello!"
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave a line after this, and also use the following after leaving the line:

It produces the following output:

Hello!


In the example above, the `newObj` is created using `Object.create()` with `prototypeObject` as its prototype. This gives it access to all of the methods on `prototypeObject` including the `greet()` function and that is why we have the final output of `Hello!` from the console.

`Object.create()` is a very useful object creation method in JavaScript. It allows you to create a new object with a specified prototype and also allows the usage of properties and functions from the prototype objecting into the new one. This aids with a clearer and more understandable code, especially when dealing with complex inheritance hierarchies. Overall, `Object.create()` provides a flexible and powerful mechanism for object creation and inheritance in JavaScript, offering advantages such as control over inheritance, encapsulation, and code organization.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Title: 'Postional parameters in sql'
Description: 'positional parameters are placeholders used in SQL statements for passing values dynamically to queries or functions. '
Subjects:
- 'Web Development'
- 'Computer Science'
- 'postgresql'
Tags:
- 'positional-parameters'
- 'Documentation'
CatalogContent:
- 'getting-started-off-platform-for-data-science'
- 'paths/design-databases-with-postgresql'
- 'paths/computer-science'

## Syntax

**Positional parameters** are often utilized in prepared statements or in dynamic SQL queries.They must always appear first in a parameter set. In access method services, positional parameters are never optional.They serve as placeholders for dynamic values that will be supplied later when executing the query or prepared statement.
When a query or prepared statement is executed, the positional parameters are replaced with actual values based on the order in which they appear and the values provided during execution.

## Example
Here's an example of positional parameters in PostgreSQL:

Suppose we have a table called employees with columns id, name, and age. We want to retrieve employees whose age is greater than a specified value, and we want to make this value dynamic using positional parameters.

First, let's create a simple table:
```sql
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name TEXT,
age INTEGER
);

INSERT INTO employees (name, age) VALUES
('John', 30),
('Alice', 25),
('Bob', 35);

```
Now, let's write a query using positional parameters to retrieve employees older than a specified age:
```sql
PREPARE get_older_employees (INTEGER) AS
SELECT * FROM employees WHERE age > $1;

```
In this query:

PREPARE creates a prepared statement named get_older_employees.
(INTEGER) specifies the data type of the positional parameter.
$1 is the positional parameter reference.
Now, we can execute the prepared statement with a specific value for the age parameter:

```sql
EXECUTE get_older_employees(25);

```
This will return all employees whose age is greater than 25.

The benefit of using positional parameters is that you can reuse the prepared statement with different parameter values without needing to rewrite the entire query. It also helps prevent SQL injection attacks by separating the query logic from the input values.