Nodejs中JS实例属性和原型属性的区别

Nodejs中JS实例属性和原型属性的区别

1.实例属性指的是在构造函数方法中定义的属性,属性和方法都是不一样的引用地址例如
function CreateObject(name,age){
this.name=name;  //实例属性
this.age=age;
this.run=function(){   //实例方法
return this.name + this.age;
}

//可以设置全局方法来实现引用地址一致性 .会出现问题
//this.run = run;

}

var box1 = new CreateObject(‘ZHS’,100); var box2 = new CreateObject(‘ZHS’,100);

console.log(box1.name == box2.name);//true console.log(box1.run() == box2.run());//true console.log(box1.run == box2.run); //false 比较的是引用地址 如何让box1,和box2run方法的地址一致呢?使用冒充 例如: /对象冒充 var o = new Object(); Box.call(o,‘xlp’,200); console.log(o.run());// 2.原型属性指的是不在构造函数中定义的属性,属性和方法都是一样的引用地址,例如 //原型 function CreateObject(){} CreateObject.prototype.name=‘ZHS’; CreateObject.prototype.age=‘100’; CreateObject.prototype.run=function(){ return this.name + this.age; }

var CreateObject1 = new CreateObject(); var CreateObject2 = new CreateObject(); console.log(CreateObject1.run == CreateObject2.run); //true console.log(CreateObject1.prototype);//这个属性是个对象,访问不了 console.log(CreateObject1.proto);//这个属性是个指针指向prototype原型对象 console.log(CreateObject1.constructor);//构造属性 如果我们在CreateObject1 添加自己属性呢?例如 CreateObject1.name=‘XLP’; console.log(CreateObject1.name);// 打印XLP //就近原则 可以使用delete删除实例属性和原型属性 delete CreateObject1.name; //删除实例中的属性 delete CreateObject.prototype.name ;//删除原型中的属性


7 回复

Node.js 中 JS 实例属性和原型属性的区别

在 JavaScript 中,理解实例属性和原型属性之间的区别对于编写高效且可维护的代码非常重要。以下是关于这两者的详细说明及示例。

1. 实例属性

实例属性是在构造函数中通过 this 关键字定义的属性和方法。每个实例都有自己独立的属性副本,因此它们的引用地址是不同的。

function CreateObject(name, age) {
    this.name = name;  // 实例属性
    this.age = age;
    this.run = function () {  // 实例方法
        return this.name + this.age;
    }
}

var box1 = new CreateObject('ZHS', 100);
var box2 = new CreateObject('ZHS', 100);

console.log(box1.name == box2.name); // true
console.log(box1.run() == box2.run()); // true
console.log(box1.run == box2.run); // false (比较的是引用地址)

在这个例子中,box1box2 都有独立的 run 方法,所以它们的引用地址不同。

如何让 box1box2run 方法引用地址一致?

可以通过将 run 方法定义在原型上实现:

function CreateObject(name, age) {
    this.name = name;
    this.age = age;
}

CreateObject.prototype.run = function () {
    return this.name + this.age;
}

var box1 = new CreateObject('ZHS', 100);
var box2 = new CreateObject('ZHS', 100);

console.log(box1.run == box2.run); // true (现在引用地址相同)

2. 原型属性

原型属性是通过构造函数的 prototype 属性定义的。所有实例共享这些属性和方法,因此它们的引用地址是相同的。

function CreateObject() {}

CreateObject.prototype.name = 'ZHS';
CreateObject.prototype.age = '100';
CreateObject.prototype.run = function () {
    return this.name + this.age;
}

var box1 = new CreateObject();
var box2 = new CreateObject();

console.log(box1.run == box2.run); // true (引用地址相同)

// 访问原型属性
console.log(box1.__proto__); // 指向 prototype 对象
console.log(box1.constructor); // 构造函数

// 在实例中添加自己的属性
box1.name = 'XLP';
console.log(box1.name); // 打印 XLP (就近原则)

// 删除实例属性和原型属性
delete box1.name; // 删除实例中的属性
delete CreateObject.prototype.name; // 删除原型中的属性

在这个例子中,box1box2 共享 run 方法,因此它们的引用地址相同。当在实例中添加或修改属性时,优先使用实例中的属性,除非显式删除。

总结来说,实例属性和方法是每个实例独有的,而原型属性和方法是所有实例共享的。理解这一点有助于更好地组织和优化代码。


支持

第一种方式会对每一个类的方法和属性独立开辟一块内存空间,而原型链的方式则仅仅是引用,很少有场景会用到第一种方式吧?

在Node.js中,JavaScript中的实例属性和原型属性有着明显的区别。理解它们之间的差异对于更好地掌握JavaScript对象的组织方式非常重要。

实例属性

实例属性是在每个对象实例创建时单独定义的。这意味着每个实例都有自己的属性副本。

function CreateObject(name, age) {
    this.name = name;  // 实例属性
    this.age = age;
    this.run = function () {   // 实例方法
        return this.name + this.age;
    }
}

var box1 = new CreateObject('ZHS', 100);
var box2 = new CreateObject('ZHS', 100);

console.log(box1.name == box2.name); // true
console.log(box1.run() == box2.run()); // true
console.log(box1.run == box2.run); // false,因为两个run方法是不同的引用

原型属性

原型属性是在对象的原型上定义的,所有通过该构造函数创建的对象都会共享这些属性。

function CreateObject() {}

CreateObject.prototype.name = 'ZHS';
CreateObject.prototype.age = '100';
CreateObject.prototype.run = function () {
    return this.name + this.age;
}

var obj1 = new CreateObject();
var obj2 = new CreateObject();

console.log(obj1.run == obj2.run); // true,因为它们共享同一个原型方法

如何让 run 方法的地址一致?

如果你想让多个实例共享同一个方法以节省内存,可以将方法定义在原型上:

function CreateObject(name, age) {
    this.name = name;
    this.age = age;
}

CreateObject.prototype.run = function () {
    return this.name + this.age;
}

var box1 = new CreateObject('ZHS', 100);
var box2 = new CreateObject('ZHS', 100);

console.log(box1.run == box2.run); // true,因为它们共享同一个原型方法

删除属性

你可以使用 delete 关键字来删除实例属性或原型属性:

obj1.name = 'XLP';
console.log(obj1.name); // XLP

delete obj1.name;
console.log(obj1.name); // ZHS,因为从原型继承了该属性

delete CreateObject.prototype.name;
console.log(obj1.name); // undefined,因为原型上的属性被删除了

通过这种方式,你可以更灵活地管理对象的属性和方法。

回到顶部