gpt4 book ai didi

javascript - OOP Javascript - 通过另一个公共(public)方法从公共(public)方法访问特权方法

转载 作者:行者123 更新时间:2023-11-28 20:26:42 24 4
gpt4 key购买 nike

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

Person.prototype.scream = function () {
this.WhatToScream.screamAge();
}

Person.prototype.WhatToScream = function () {
this.screamAge = function () {
alert('I AM ' + this.age + ' YEARS OLD!!!');
}
this.screamName = function () {
alert('MY NAME IS ' + this.name + '!!!')
}
}

var man = new Person('Berna', 21);
man.scream();


// This code raises:
// Uncaught TypeError: Object WhatToScream has no method 'screamAge'

最佳答案

这是更接近原始代码的重新定义:

Person.prototype.scream = function () {
new this.WhatToScream().screamAge();
}

关于javascript - OOP Javascript - 通过另一个公共(public)方法从公共(public)方法访问特权方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17248146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com