Discover javascript class vs prototype, include the articles, news, trends, analysis and practical advice about javascript class vs prototype on alibabacloud.com
/* Source author: stone not Easy (Louisshi) contact information: http://www.shibuyi.net============================================= ====================================================== Program name:javascript Package Library Prototype version Iteration : non-functional total:14 features:1. implementation code concatenating 2.id/name/tagname/ class node Get 3.class
In "JavaScript Play Inheritance (ii)", I used the prototype inheritance method to implement JavaScript inheritance, and the prototype is what the mystery. In this article, I will focus on the prototype to start the discussion.
Aside from
(the object is created in literal form).
function Foo () {};
var foo = new Foo ();
Console.log (foo.__proto__);
Just imagine, what does the __proto__ of this Foo object point to?
An object that contains the constructor property? Do not understand that it does not matter, the function of Foo's prototype attributes printed out, compared to know.
function Foo () {};
var foo = new Foo ();
Console.log (foo.__proto__);
Console.log
When defining a JS class, why set the prototype property of the class to the instance object of the class to which it inherits
When writing JavaScript class definitions, it is likely that you have written the following code:
fun
The relationship between prototype and _ proto _ in Javascript is described in detail. prototype _ proto _
Preface
When I learned the prototype, I felt my head was too big )/~~ In particular, prototype and _ proto _ are too stupid to find out more information. Based on your
unnecessary attributes. At the same time, the prototype chain can remain unchanged, so instanceof and isPrototypeof () can be used normally (). This is also the most ideal inheritance method currently, and is also transforming to this mode. (YUI also uses this mode .)
This blog reference JavaScript advanced programming version 3rd, the code is rewritten, more specific, and added annotations to make everyon
The prototype attribute of the object in JavaScript, which can return the reference of the Object Type prototype. This is a pretty straightforward explanation. To understand it, you must first correctly understand the concepts of object types and prototypes.1. What is prototype?The
I have been studying js recently and have many questions. It is confused by the prototype. The question comes from:
The Code is as follows:
Function foo {This. name = 'foo ';}Alert (foo. prototype = Function. prototype); // false. At that time, I never figured out why the prototype of foo is not Function.
Every object in JavaScript has a prototype property.Object methods, class methods, prototype methods1, Object method: Understanding is very simple, mainly if the class generates an instance, then the instance can use this method2. Class
For the new, JavaScript prototype is a very annoying thing, one prototype easy to confuse with __proto__, and the various points between them is really a bit complicated, in fact, there have been a lot of articles on the market to try to say clearly, there is a so-called very classic diagram, The above painted a variety of lines, one will connect this one will co
(Firstprisoner.sentence);
But the object's prototype did not change, and the value was still 4 console.log (firstprisoner.__proto__.sentence);
Removes the delete firstprisoner.sentence from the object in order to get the property back to the value of the prototype; Next, the JavaScript engine cannot find the attribute on the object,//Must go back and look up t
Prototype
As you all know, JavaScript does not contain the traditional class inheritance model, but instead uses the prototype prototype model. The code implementation is probably like this
function Student (name) {
this.name = name;
}
var kimy = new Student ("Ki
likely to cause a memory overflow. There is no way to create only one talk (), the answer is YES! Just use prototype!.Take a look at the code first:The same class that defines a personvar person = function (name,age) {Specifies that the property of the class is a parameter valueTHIS.name = name;This.age = age;We don't set the talk method for person here anymore.
JavaScript three types of methods:1, class method, 2, Object method, 3, prototype method, attention three similarities and differencesCases:1 functionpeople (name) {2 This. name=name;3 //Object Methods4 This. Introduce=function(){5Console.log (' My Name is ' + This. Name);6 }7 }8 9 //class MethodTenpeople.run=functio
this object to find its properties. */function Employee (name, salary) {THIS.name = name;This.salary = salary;}/* All objects of the employee have the Getsalary () and Addsalary () methods, both of which are unique and shared by this prototype object.All objects that are not employee have this method (not one by one correspondence, but many to one) */Employee.prototype.getSalary = function getsalaryfunction () {return this.salary;}Employee.prototype.
JavaScript are created by object, and functions are created by function. Conclusion 3: The built-in object is actually a function object, which is created by functions. object.__proto__ = = = Function.prototype; Conclusion 4:each object or function in JS has a __proto__ property, but only the function object has the prototype property. function object Functions Person () {}//Ordinary object var obj = {};ob
Before I was in the prototype chain of JavaScript, prototype inheritance and identifier lookup were somewhat confusing,
For example, the following code:
Copy Code code as follows:
function Foo () {};
var foo = new Foo ();
Foo.prototype.label = "Laruence";
alert (Foo.label); Output:laruence
alert (Foo.label);//output:undefined
To
------------------------------the prototype chain method (prototype) inherits all the methods of the parent class, and the call method is the clone this invocation method---------------------function Saycolor (Sprefix, Ssuffix) {Alert (Sprefix + This.color + ssuffix)}var obj = new Object ();Obj.color = "Red";Saycolor.call (obj, "The color is", ", a ver nice color
Inheritance in JavaScript is accomplished through a prototype chain (prototype chain): There is another object inside each object that exists as its prototype, and the object inherits the property from the prototype. For each object, you can access its
Understand the following diagram to understand JS prototype, prototype chain1, each object has its own prototype object, each object has a prototype property, which is a pointer to the object's prototype object, the prototype obje
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.