Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js面向对象 #10

Open
caimiaoyan opened this issue Nov 4, 2020 · 0 comments
Open

js面向对象 #10

caimiaoyan opened this issue Nov 4, 2020 · 0 comments

Comments

@caimiaoyan
Copy link
Owner

caimiaoyan commented Nov 4, 2020

1.对象模型的细节
2.继承与原型链
3.call() 和 apply()
4.Object.creat()
5.Object.create()、new Object()和{}的区别
6.详解JS构造函数方法与原型prototype上的方法
7. instanceof()
8. setPrototypeOf()

function A(){
}
var obj = new Object(); // 创建一个空对象
obj.__proto__ = A.prototype; // obj的__proto__指向构造函数的prototype
var result = A.call(obj); // 把构造函数的this指向obj,并执行构造函数把结果赋值给result
if (typeof(result) === 'object') {
    A = result; // 构造函数F的执行结果是引用类型,就把这个引用类型的对象返回给objB
} else {
    A = obj; // 构造函数F的执行结果是值类型,就返回obj这个对象给objB
}

prototype是构造函数方法特有,与new关键字配合使用,每个实体对象都有__proto__属性

function Product(name, price) {
    this.name = name;
    this.price = price;
    return {
        name: this.name,
        color: 'red'
    }
}

var objectFactory = function() {

    var obj = new Object(),
        Constructor = [].shift.call(arguments);

    obj.__proto__ = Constructor.prototype;

    var ret = Constructor.apply(obj, arguments);

    return typeof ret === 'object' ? ret : obj;

};

var newProduct = new Product('name', 'price');
// var newProduct = objectFactory(Product, 'name', 'price');

console.log(newProduct)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant