Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

javascript - When I assign prototype to function I get undesired output

Please follow the code below,

var fn79 = function(){

    var Student = function(_name){
        this.name = _name;
    };


    Student.prototype = function(){
        print("Inside Prototype function");
    };


    //Student.prototype = {}

    var obj1 = new Student("Ricky");

    Student.prototype.lastName = "Gonzales";

    var obj2 = new Student("Jacky");

    print(obj1.name+" - "+obj1.lastName);
    print(obj2.name+" - "+obj2.lastName);   
};

fn79();

The output I get is

D:Rahul ShivsharanMyPractiseJavaScriptCommandLine>java -jar js-14.jar prac.js
- Gonzales
- Gonzales

D:Rahul ShivsharanMyPractiseJavaScriptCommandLine>

From above output you can see I am not able to print "name" property of objects.

now if I change the the code to as below,

    var fn79 = function(){

    var Student = function(_name){
        this.name = _name;
    };

    /*
    Student.prototype = function(){
        print("Inside Prototype function");
    };
    */

    Student.prototype = {}

    var obj1 = new Student("Ricky");

    Student.prototype.lastName = "Gonzales";

    var obj2 = new Student("Jacky");

    print(obj1.name+" - "+obj1.lastName);
    print(obj2.name+" - "+obj2.lastName);   
};

fn79();

I get the desired output as

D:Rahul ShivsharanMyPractiseJavaScriptCommandLine>java -jar js-14.jar prac.js
Ricky - Gonzales
Jacky - Gonzales

D:Rahul ShivsharanMyPractiseJavaScriptCommandLine>

Why my First example was not working properly.

Function is an object itself in javascript.

What I thought is,

Student.prototype = function(){
       print("Inside Prototype function");
}

Student's prototype is pointing to function which itself is an object.

So why "name" is not getting printed in my first case, and how prototype assigning to function effects it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem with functions is that they do have a non-writable .name property. This non-writability even affects the objects that inherit from the function, in your case the Student instances, so the assignment fails:

var Student = function(name){
    console.log(this.name);
    this.name = name;
    console.log(this.name);
};
Student.prototype = function ProtoName() {};
console.log(Student.prototype.name);
new Student("test");

You'll just see the string "ProtoName" thrice.

You can use strict mode to make it more obvious that the .name = fails:

var Student = function(name){
    "use strict";
    this.name = name;
};
Student.prototype = function ProtoName() {};
new Student("test");

You'll get an Error: Invalid assignment in strict mode.

You can work around the non-writability by creating the .name property on the Student instance using Object.defineProperty instead of the simple assignment, but really you just should not use a function object as a prototype.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...