| |

VerySource

 Forgot password?
 Register
Search
View: 2440|Reply: 11

What exactly is a prototype? Why use prototype?-Novice ask ...

[Copy link]

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-18 22:40:02
| Show all posts |Read mode
function Car ()
{}
Car.prototype.color = "red";
Car.prototype.doors = 4;
Car.prototype.mpg = 23;
Car.prototype.showColor = function ()
{
    alert (this.color);
};
var oCar1 = new Car ();
var oCar2 = new Car ();


my question is:
Why use prototype here?
Why can't it be written directly like this:
function Car ()
{}
Car.color = "red";
Car.doors = 4;
Car.mpg = 23;
Car.showColor = function ()
{
    alert (this.color);
};
var oCar1 = new Car ();
var oCar2 = new Car ();

And why is the prototype written outside the function body?
What's wrong with it?
function Car ()
{
Car.prototype.color = "red";
Car.prototype.doors = 4;
Car.prototype.mpg = 23;
Car.prototype.showColor = function ()
{
    alert (this.color);
};
}
Reply

Use magic Report

0

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-1-25 21:45:02
| Show all posts
Just use prototype as a function.
Writing js is generally writing functions, but some functions are directly written into prototypes with high reusability.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-26 08:09:01
| Show all posts
I just find it hard to understand it. You say it is a function, but it is not like a function ...
So for the novice me, I really "stuck" here.

So can you explain it in more depth?
Thank you! !! !!
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-26 14:54:01
| Show all posts
It is a good API written in Javascript, including extensions to Javascript classes such as String, Array, etc. It can be used directly after embedding the JS file. If the program does not understand, I suggest you refer to JSON and go online Check it out, there are many such articles, prototype uses JSON syntax.
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-26 22:54:02
| Show all posts
Probably as written in the book, it can effectively save space; otherwise, create a few more objects, and some unnecessary methods and data will be repeatedly created ~ I understand so
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-30 03:54:01
| Show all posts
bear4ever's prototype is not the same as the landlord,bear4ever's prototype is a JS framework. The prototype is used to inherit the prototype object, mainly to save memory space.
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-3-3 22:30:02
| Show all posts
mark
Reply

Use magic Report

0

Threads

4

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-12 23:00:01
| Show all posts
Someone asked it before, and then a senior said that the memory distribution is different. . . .
I ca n’t find the address (it ’s hard to find, so I wo n’t find it) ^ o ^)
Reply

Use magic Report

0

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-3-19 09:30:02
| Show all posts
The relationship between class and implementation is similar to the relationship between mold and finished product. The prototype is the detail carving of the mold, and what will be engraved, then what will be the finished product cast from this mold.
Car.doors = 4; This way of writing only extends some static members of the Car object, and will not be inherited by the instance.
Reply

Use magic Report

1

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-4-10 11:15:01
| Show all posts
Car.doors = 4; This way of writing only extends some static members of the Car object, and will not be inherited by the instance.


Thank you7660367
It's a lot clearer if you say this ~~~

But there is still a problem
Why should the prototype be written outside the function body?
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list