在Javascript中使用"this"和"prototype"之间有区别吗?

pen*_*ake 5 javascript prototype this keyword

以下两个代码之间是否存在差异,我认为不是.

function Agent(bIsSecret)
{
    if(bIsSecret)
        this.isSecret=true;

    this.isActive = true;
    this.isMale = false;
}
Run Code Online (Sandbox Code Playgroud)

function Agent(bIsSecret)
{
    if(bIsSecret)
        this.isSecret=true;
}

Agent.prototype.isActive = true;    
Agent.prototype.isMale = true;
Run Code Online (Sandbox Code Playgroud)

Luk*_*man 2

至少如果您将非原始对象分配给this.somevaror ,则存在差异prototype.somevar

尝试运行这个:

function Agent(bIsSecret)
{
    if(bIsSecret)
        this.isSecret=true;

    this.isActive = true;
    this.isMale = false;
    this.myArray = new Array(1,2,3);
}

function Agent2(bIsSecret)
{
    if(bIsSecret)
        this.isSecret = true;
}

Agent2.prototype.isActive = true;    
Agent2.prototype.isMale = true;
Agent2.prototype.myArray = new Array(1,2,3);

var agent_a = new Agent();
var agent_b = new Agent();

var agent2_a = new Agent2();
var agent2_b = new Agent2();

if (agent_a.myArray == agent_b.myArray) 
    alert('agent_a.myArray == agent_b.myArray');
else
    alert('agent_a.myArray != agent_b.myArray');

if (agent2_a.myArray == agent2_b.myArray) 
    alert('agent2_a.myArray == agent2_b.myArray');
else
    alert('agent2_a.myArray != agent2_b.myArray');
Run Code Online (Sandbox Code Playgroud)