


Examples of instance objects and prototype objects in JavaScript_Basic knowledge
First of all, declare: Every object in JavaScript has a constructor attribute and a prototype attribute. constructor points to the object's constructor, and prototype points to the prototype object of the object instance created using the constructor.
function Person(){ } var person = new Person(); Person.prototype = { constructor : Person, name : 'zxs', age : 24, sayName : function(){alert(this.name)} } person.sayName();
An error will be reported in this code, sayName() is not defined. According to JavaScript Advanced Programming Second Edition, it is because the overridden prototype cuts off the connection between the constructor and the original prototype. But let’s adjust the order of the above statements. As follows:
function Person(){ } //var person = new Person(); Person.prototype = { constructor : Person, name : 'zxs', age : 24, sayName : function(){alert(this.name)} } /*===========================================================*/ var person = new Person(); /*===========================================================*/ person.sayName(); // zxs alert(person.constructor) //function Object() { [native code]} or function Person() {} 取决与蓝色的语句是否有效
Pay attention to the statements between the equal signs in the two pieces of code above. If you write the code in the order of the second paragraph, "zxs" will be output. This result shows that the error reported in the first case does not mean that it is caused by cutting off the connection between the constructor and the original idea.
Person.prototype = {}
It is originally a way to define objects, and the constructor property of each object in JavaScript points to the Object constructor by default. This is not difficult to show that rewriting the prototype object does cut off the connection between the constructor and the original prototype. connection, but it does not mean that person cannot access the sayName() function after this connection is severed.
Now there is this assumption: the prototype object pointed to by the prototype attribute of the function is not exactly the same as the prototype object we display to create a new one. When we call a function, a prototype object will be created. At this time, we will first check whether its prototype object exists in the current environment. If it does not exist in the program, create one. If it exists in the environment, we will search for their properties and methods. Finally, A prototype object is returned based on the search result. The properties and methods in this object always use the properties and methods in the default prototype first, that is, the properties and methods defined in the constructor. When the called method or property does not exist in the default prototype, the properties and methods defined in Person.prototype = {} are used.
Javascript is an interpreted language, and statements are executed sequentially. In the first piece of code, when we use the new keyword to create a new object, Person.prototype = {} is not executed, which means that The methods and properties defined in it cannot be found in the current execution environment, and the method does not exist in the constructor, so an error occurs. Just like a variable, it cannot be used when the program is not executed when assigning a value to it. In the second paragraph, the called method already exists in the environment, and the prototype object of the constructor has been created, so the result can be obtained.
Look at the following program:
////////////////////////////////////////////////////////////////////////// function Person(){} /*===========================================================*/ var person = new Person(); Person.prototype.name = 'song'; /*===========================================================*/ //Person.prototype.sayName = function(){alert(this.name)}; Person.prototype = { constructor : Person, name : 'zxs', age : 24, sayName : function(){alert(this.name)} } person.sayName(); // error ////////////////////////////////////////////////////////////////////////// function Person(){ } /*var person = new Person();*/ Person.prototype.name = 'song'; /*Person.prototype.sayName = function(){alert(this.name)};*/ Person.prototype = { constructor : Person, name : 'zxs', age : 24, sayName : function(){alert(this.name)} } /*===========================================================*/ var person = new Person(); /*===========================================================*/ person.sayName(); // zxs
It can be seen from here that using Person.prototype.name = '', the object can be accessed no matter where it is created. If there is both an object literal and a prototype object defined by this method, the later defined object will be used. as final value. And after using an object literal definition for a prototype object, the definition must appear before the statement that creates the object before it can be accessed.
Instances cannot access properties and methods in the prototype object, not least because overriding the prototype object cuts off the connection between the constructor and the original prototype.
function Person(){ } var person = new Person(); Person.prototype = { //constructor : Person, name : 'zxs', age : 24, sayName : function(){alert(this.name)} } person.sayName();
The prototype of the constructor in the above code is empty when instantiating the object, and it does not have any properties other than the default properties. Overriding the constructor's prototype does sever the connection between the constructor and the original prototype.
After using the new operator, the properties and methods in the prototype object of the constructor have been added to the person object. Because the above method is not dynamic in adding new properties and methods to the function prototype, person cannot access the newly added properties and methods.
After rewriting the prototype object, it is like the following code:
var o = { name : 'zxs' } var obj = o; o = {} console.log(o.name);
The output value at this time is undefined, because the object is a reference type, "=" is the assignment operator, and the order of operations is from right to left. o={} means that the pointing of o has changed and is an empty object.
The difference between Person.prototype.mothed = function() {} and Person.prototype={mothed:function(){}} is the same as arr = [] and arr.push(). The former modifies itself, while the latter modifies itself. Completely transform yourself.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The Request object in PHP is an object used to handle HTTP requests sent by the client to the server. Through the Request object, we can obtain the client's request information, such as request method, request header information, request parameters, etc., so as to process and respond to the request. In PHP, you can use global variables such as $_REQUEST, $_GET, $_POST, etc. to obtain requested information, but these variables are not objects, but arrays. In order to process request information more flexibly and conveniently, you can

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

In C++, there are three points to note when a function returns an object: The life cycle of the object is managed by the caller to prevent memory leaks. Avoid dangling pointers and ensure the object remains valid after the function returns by dynamically allocating memory or returning the object itself. The compiler may optimize copy generation of the returned object to improve performance, but if the object is passed by value semantics, no copy generation is required.

Golang is a powerful and efficient programming language that can be used to develop various applications and services. In Golang, pointers are a very important concept, which can help us operate data more flexibly and efficiently. Pointer conversion refers to the process of pointer operations between different types. This article will use specific examples to learn the best practices of pointer conversion in Golang. 1. Basic concepts In Golang, each variable has an address, and the address is the location of the variable in memory.

The relationship between the number of Oracle instances and database performance Oracle database is one of the well-known relational database management systems in the industry and is widely used in enterprise-level data storage and management. In Oracle database, instance is a very important concept. Instance refers to the running environment of Oracle database in memory. Each instance has an independent memory structure and background process, which is used to process user requests and manage database operations. The number of instances has an important impact on the performance and stability of Oracle database.
