js中创建对象的几种方式
2021-01-05 08:31
标签:his 圆形 walk class fun nbsp bsp return ret js中创建对象的几种方式 标签:his 圆形 walk class fun nbsp bsp return ret 原文地址:https://www.cnblogs.com/yyk520/p/13617352.html/*字面量的形式创建*/
var yyk = {
name:"yyk",
age:24
}
/*通过构造函数创建*/
var yyk = new Object();
yyk.sex=‘男‘;
/*通过工厂创建*/
function createYYK(name,age,sex) {
let o = new Object();
o.name=name;
o.age=age;
o.sex=sex;
o.sayName=function () {
alert(name)
}
return o;
}
/*通过构造函数创建*/
function YYK(name,age,sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
/*通过圆形模式创建*/
function YYK() {}
YYK.prototype.age="24";
YYK.prototype.name="24";
YYK.prototype.sex="24";
/*prototype可以给YYK添加新的属性*/
/*prototype实现继承*/
function Persion(name,age,sex) {
this.name=name;
this.age=age;
this.sex=sex;
}
function Student() {
}
Student.prototype = new Persion();//子类的对象用父类声明
Student.prototype.walk=function () {
alert("正在走")
}
!function () {
let yYk = new YYK("yyk",24,"男");
let p = createYYK("yyk",24,"男")
let student = new Student();
alert(student.sex)
}();