IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

JS原型与原型链

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

原型与原型链

每日更新前端基础,如果觉得不错,点个star吧
https://github.com/WindrunnerMax/EveryDay

JavaScript有着七种基本类型StringNumberBooleanNullUndefinedSymbolObject,前六种为基本数据类型,Object为引用类型。函数本质上是Object类型,也就是一个对象。

值得注意的是typeof (null)会返回Object,这是因为JS二进制前三位都为0的话会被判断为Object类型,null的二进制表示是全0,自然前三位也是0,所以执行typeof时会返回Object,实际null为基本数据类型。

构造函数对象

构造一个Student类,实例化Studentstu实例对象

function Student() {}
var stu = new Student();
stu.name = "Ming";
console.log(stu.name) // Ming

prototype

每个函数对象都会有一个prototype属性,prototype就是调用构造函数所创建的那个实例对象的原型,prototype可以让所有对象实例共享它所包含的属性和方法。

function Student() {}
Student.prototype = {
    from: "sdust"
}
var stu1 = new Student();
var stu2 = new Student();

console.log(stu1.from) // sdust
console.log(stu2.from) // sdust

__proto__

__proto__是原型链查询中实际用到的,它总是指向prototype,就是指向构造函数Student的原型对象prototype。例如实例化的stu会使用__proto__Studentprototype寻找方法或属性。若stu寻找到了调用的方法或属性,则不会使用__proto__寻找原型对象。

function Student() {}
Student.prototype = {
    from: "sdust"
}
var stu = new Student();


console.log(stu.__proto__ === Student.prototype) // true
console.log(stu.from) // sdust

stu.from = "s";
console.log(stu.from) // s

constructor

每个原型都有一个constructor属性指向关联的构造函数Student,实例的constructor指向构造函数Student

function Student() {}
var stu = new Student();

console.log(Student.prototype.constructor === Student) // true
console.log(stu.constructor === Student) // true

原型链

原型链可以简单理解为将原型连成一条链,js每一次获取对象中的属性都是一次查询过程,如果在自有属性中找不到就会去原型对象中查找,如果原型对象中还查不到,就回去原型对象的原型中查找,也就是按照原型链查找,直到查找到原型链的顶端,也就是Object的原型。

function parent() {
    this.parentInfo = "parent";
}

parent.prototype.getParentInfo = function() {
    return this.parentInfo;
};

function child(){
    this.childInfo = "child";
}

parent.prototype.getChildInfo = function() {
    return this.childInfo;
};

child.prototype = new parent();

var instance = new child();

console.log(instance.getChildInfo()); // child
console.log(instance.getParentInfo()); // parent
console.log(instance.parentInfo); // parent
console.log(instance.__proto__ === child.prototype); //true
console.log(instance.__proto__.__proto__ === parent.prototype); //true
console.log(instance.__proto__.__proto__.__proto__ === Object.prototype); //true
console.log(instance.__proto__.__proto__.__proto__.__proto__ === null); //true


// function student(){}
// console.log(student.__proto__ === Function.prototype) // true
// console.log(Function.__proto__ === Function.prototype) // true
// console.log(Object.__proto__ === Function.prototype) // true
// console.log(Object.prototype.__proto__ === null) // true

文章永久链接:https://tech.souyunku.com/?p=39564


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(92) 打赏



未经允许不得转载:搜云库技术团队 » JS原型与原型链

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367