js面向对象编程
学过java的都知道对象,类的区别以及在代码中的相关使用,但是js中没有类的概念,只有对象的概念,下面附上代码
function ptotoTest(name,age){
this.name = name;
this.age = age;
};
ptotoTest.prototype.sex= "male"
var demo2 = new ptotoTest("zhangch",20);
console.log(demo2.sex);
console.log(demo2.__proto__);
console.log(ptotoTest.prototype);
大家知道打印出来的结果分别是啥吗? male,ptotoTest {sex: "male"},ptotoTest {sex: "male"}
prototype 用来给函数对象添加一个属性,protototype的具体用法可以自己去查看一下,下面的代码是创建对个对象的时候,属性的使用方式,之前创建的对象不能使用后面的属性
function ptotoTest(name,age){
this.name = name;
this.age = age;
};
var demo1 = new ptotoTest("liuzz",18);
console.log(demo1.sex);
ptotoTest.prototype.sex= "male"
var demo2 = new ptotoTest("zhangch",20);
console.log(demo2.sex);