search
HomeWeb Front-endJS TutorialThe relationship between property and prototype of javascript objects

ECMAScript can recognize two types of objects, one is called Native Object and belongs to the language category; the other is called Host Object, which is provided by the running environment such as document object, Dom Node, etc.
Native objects are a loose structure and can be added dynamically Properties, all properties have a name and a value. This value can be a reference to another object or a built-in data type (String, Number, Boolean, Null or Undefined)
The following simple example Describes how a JavaScript object sets the value of a property and how to read the value of the property.

Assignment operation
Creating the attributes of an object is very simple. The creation of attributes can be completed directly through the assignment operation.


Code
var objectRef = new Object(); //create a generic javascript object.

A property named testNumber can be created like this.

Code
objectRef.testNumber = 5;
/* - or:- */
objectRef["testNumber"] = 5;

If the copied attribute name already exists, the attribute will not be created again, and the assignment operation is just Reset the value of the attribute

Code
objectRef.testNumber = 8;
/* - or:- */
objectRef["testNumber"] = 8;


The prototype of a js object can itself be an object , it can also have attributes (property). The assignment operation of js object (prototype) is no different from the creation of ordinary object properties
.

Value operation
In the value operation, property and prototype are different. Let’s look at the simplest property value operation first.

Code
/*Assign a value to the attribute of an object. If the object does not have this attribute, then after the assignment operation, the object will have this attribute*/
objectRef.testNumber = 8;
/* Read out the attribute Value*/
var val = objectRef.testNumber;

/* Now val has the value 8 just assigned to objectRef*/

prototype revealed

But all objects can have prototypes, and prototypes themselves are also objects. Then he can also have prototypes, and a prototype chain will be formed by looping in this way.
This chain will terminate when it encounters that the prototype of the formation in the chain is null. (The default prototype of Object is null)

Code
var objectRef = new Object(); //create a generic javascript object.

Create a new js object. At this time, the prototype of this object is Null, so the objectRef The prototype chain only contains one object Object.prototype
We are looking at the following code

Code
/* Construct the constructor of the type MyObject1 ​
MyObject1 - type.
*/
function MyObject1(formalParameter){ ​
​ /* For the object Create a property named testNumber
*/
this.testNumber = formalParameter;
}

/* Construct the constructor of the MyObject2 type
MyObject2 - type:-
*/
function MyObject2(formalParameter){
/* The one who does it The object creates a property named testString*/
this.testString = formalParameter;
}

/* The next step will replace the default prototype property of MyObject2 with the MyObject1 object*/
MyObject2.prototype = new MyObject1(8);

/* Finally we create an object of type MyObject2*/

var objectRef = new MyObject2( "String_Value" );

objectRef This object of type MyObject2 has a prototype chain, and the first object in the chain is MyObject1 Object, MyObject1 object also has a prototype,
This prototype is the default prototype of Object, the prototype of Object.prototype is null, and this prototype chain ends.
When a value operation occurs, the entire prototype chain of objectRef starts to work

Code
var val = objectRef.testString;

objectRef has an attribute called testString, then this code will assign the value of testString Give val

code
var val = objectRef.testNumber;

There is no attribute testNumber in the objectRef object, but val has reached the value 8 instead of undefine. This is because the interpreter has not found the required value in the current object. After finding the attribute of
, we will check the prototype of this object. The prototype of objectRef is the MyObject1 object. This object has the attribute testNumber, so val gets the value 8.

Code
var val = objectRef.toString;

Now val is a reference to a function. This function is a property of Object.prototype. Since neither MyObject1 nor MyObject2 defines the toString property
so Object.prototype returns.

Code
var val = objectRef.madeUpProperty;

The last val is undefined, because MyObject1, MyObject2, and Object have not defined the property madeUpProperty, so the result is undefined.

The read operation will read in obj itself and the first property value with the same name found on the prototype chain
The write operation will create a property with the same name for the obj object itself (if this property name does not exist
This means objectRef.testNumber = 3 will create a property on the objectRef object, The name is testNumber. When you want to read testNumber next time, the
propertype chain will not work. You will only get the property 3 of objectRef, and the testNumber property of MyObject1 will not be modified. The following code can be verified


code
/ * Construct the constructor of the type MyObject1
MyObject1 - type.
*/
function MyObject1(formalParameter){
/* Create a property named testNumber for the object
*/
this.testNumber = formalParameter;
}

/ * Construct the constructor of the type MyObject2
MyObject2 - type:-
*/
function MyObject2(formalParameter){
/* Create a property name for the object named testString*/
this.testString = formalParameter;
}
/ * The next operation will replace the default prototype attribute of MyObject2 with the MyObject1 object*/
var obj1 = new MyObject1(8);
MyObject2.prototype = obj1;

/* Finally we create an object of type MyObject2*/

var objectRef = new MyObject2( "String_Value" );

alert(objectRef.testNumber);
objectRef.testNumber = 5;
alert(objectRef.testNumber);
alert(obj1.testNumber); More related articles Please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools