the for-in loop, returns all the accessible, enumerable properties in the instance and prototype .functionPerson (name,age) { This. Name =name; This. Age =Age ;} Person.prototype.sayName=function() {Console.log (' Hello, I call: ' + This. Name + '. Gender: ' + This. sex);}; Person.prototype.sex= ' Male ';varP1 =NewPerson (' Xiao Ming ', 25);p 1.sayName (); //Hello, my name is: Xiao Ming. Gender: Male for(varIinchp1) { if(P1.hasownproperty (i)) {conso
Object-oriented, that is, according to the human way of thinking to write programs, this is the innate way of thinking and not a new approach.1 . Object: True existence in memory;2, the object is to look at things is an object composition but objects have properties and methods.3, Class: Abstract of a set of objects wi
Brief prototype:In JS, each constructor has a prototype attribute prototype, because the value of this property is usually an object, also known as a prototype Object! You do not need to explicitly define the prototype
In JavaScript, the prototype object I think is still more obscure.In JS we can think of JS is the face of the object, and Java almost, I am also Java origin. The understanding of objects in JS and Java and other strong types of language is not the same, so it will cause a lot of back-end staff learning JS This piece wi
The meaning of prototype in the software world is the original form of the Table object. The prototype in JavaScript.All objects of the function type of JS have a prototype property. This prototype property itself is an object typ
"; Person2.friends.push ("Durrant"); alert (person1.friends); //output: John Doe, Harry, Zhao Liu, DurrantThis inheritance pattern is similar to the prototype constructor pattern, except that here the F constructor acts as a base object, and his prototype object is dynamically changing according to the incoming
This article mainly introduces how to use Prototype to implement object-oriented methods in JavaScript. examples show how to use Prototype attributes, which is of great practical value, for more information about how to use Prototype to implement
Referring to constructors, we are not unfamiliar, in the object-oriented domain, the constructor is already a commonplace problem. Learning again in JavaScript really has a kind feeling. one, simple review Constructor, is a special method. Used primarily to initialize objects when they are created, that is, object member variable Assign initi
JavaScript uses Prototype to implement the object-oriented method. javascriptprototype
This example describes how JavaScript uses Prototype to implement object-oriented methods. Share it with you for your reference. The specific
We have used the prototype property in the first chapter to simulate the implementation of classes and inheritance. The prototype property is essentially a JavaScript object. And each function has a default prototype property.
If this function is used in a scene that creates
leakage, because every time a new instance is generated, all attributes and methods are copied, occupying a large amount of memory.Memory is the first consideration for large JavaScript applications.The correct method is to use the prototype keyword to define the methods or attributes of a class.Copy codeThe Code is as follows: var myobject = function (param1, param2){This. name = param1;This. age = param2
JavaScript does not have the concept of classes, but object-oriented flags do have a class concept. There is no way for a solution with class abstraction repetition to be implemented through inheritance in JavaScript. But every function of JavaScript automatically adds a name called the
This article is an example of JavaScript-object-oriented shared member properties and methods and prototype keyword usage. Share to everyone for your reference. Specifically as follows:
Shared member properties and methods, using prototype keywords
Copy Code code as follows:
Attention to knowledge points:
There are three of objects in JavaScript1, internal objects
such as array,boolean,data,math,number,object,regexp,string objects, etc.
These object systems provide us with their own properties and methods for invocation.
2, class-based objects
Implements references to objects in the form of classes that require our own definition
3, based on the prototype
First code:
function Human () {}
Human.prototype.name = "Genghis Khan";
Human.prototype.sex = "male";
Human.prototype.say = function () {
alert (this.name);
var man = new Human ();
Man.say (); Genghis Khan
var woman = new Human ();
Woman.say (); Genghis Khan
alert (Man.say = = Woman.say);//true
Prototype is one of the very core points of knowledge in JavaScript. He is an important part of the
"]; } Person.prototype = { Constructor:person, sayname:function () { alert (this.name) } } var Person1 = new Person (' lilei ', +, ' software Engineer '); var person2 = new Person (' Greg ', ' Doctor '); Person1.friends.push ("Van"); alert (person1.friends); alert (person2.friends); Alert (person1.friends = = = Person2.friends); Alert (Person1.sayname = = = Person2.sayname);In this example, the instance properties are defined in the constructor, and the
Notes on javaScript object-oriented and prototype
JavaScript is a special language. ECMAScript does not have the class concept and is different from other object-oriented languages, its objects are also different from those in class-based languages. Strictly speaking,
In front we pave a lot of details, is to let everyone more clear prototype use details;Now you can combine the previous knowledge and write a function to quickly build an inheritance relationship based on the prototype chain:function extend (child, Parent) {var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F (); Child.prototype.constructor = child; Child.uber = Parent.prototype;}
every time a new instance is generated, all attributes and methods are copied, occupying a large amount of memory.Memory is the first consideration for large JavaScript applications.The correct method is to use the prototype keyword to define the methods or attributes of a class.Copy codeThe Code is as follows:Var myobject = function (param1, param2){This. name = param1;This. age = param2;Myobject.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.