最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you put an ifelse statement inside an object key? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to create a Person class. The person's age would be a random number, determined by an if/else statement. Right now it seems to only work if I place the function outside of the object, or as a separate key.

function age(x) {
    if (x.toLowerCase().charCodeAt(0) <= "g".charCodeAt(0)) {
        return Math.floor(Math.random()*40+1);
    }
    else {
        return Math.floor(Math.random()*40+41);
    }
}

function person(name) {
    this.name = name;
    this.age = age(name);
}

var people = {
    joe: new person("Joe")
};

console.log(people.joe.age);
\\ returns a number 41-80

Is there a way for me to put the function directly into the "this.age" key and have the same thing happen, like so:

function person(name) {
    this.name = name;
    this.age = function age() {
        if (this.name.toLowerCase().charCodeAt(0) <= "g".charCodeAt(0)) {
            return Math.floor(Math.random()*40+1);
        }
        else {
            return Math.floor(Math.random()*40+41);
        }
};

I'm trying to create a Person class. The person's age would be a random number, determined by an if/else statement. Right now it seems to only work if I place the function outside of the object, or as a separate key.

function age(x) {
    if (x.toLowerCase().charCodeAt(0) <= "g".charCodeAt(0)) {
        return Math.floor(Math.random()*40+1);
    }
    else {
        return Math.floor(Math.random()*40+41);
    }
}

function person(name) {
    this.name = name;
    this.age = age(name);
}

var people = {
    joe: new person("Joe")
};

console.log(people.joe.age);
\\ returns a number 41-80

Is there a way for me to put the function directly into the "this.age" key and have the same thing happen, like so:

function person(name) {
    this.name = name;
    this.age = function age() {
        if (this.name.toLowerCase().charCodeAt(0) <= "g".charCodeAt(0)) {
            return Math.floor(Math.random()*40+1);
        }
        else {
            return Math.floor(Math.random()*40+41);
        }
};
Share Improve this question asked Feb 28, 2013 at 23:05 KoreyKorey 891 silver badge6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

You can execute the function immediately:

function person(name) {
    this.name = name;
    this.age = (function age() {
        if (this.name.toLowerCase().charCodeAt(0) <= "g".charCodeAt(0)) {
            return Math.floor(Math.random()*40+1);
        }
        else {
            return Math.floor(Math.random()*40+41);
        }
    })();
};
function person(name) {
    this.name = name;
    this.age = (function age() {
        var x = this.name.toLowerCase().charCodeAt(0) <= "g".charCodeAt(0))?1:41;
        return Math.floor(Math.random()*40+x);
        })();
};

doing (function(){})() you're executing it.

(function(){}) //this converts the function into a statement
() // this executes

You have to define the closure (function) and execute it right on.

  function person(name) {
        this.name = name;
        this.age = (function age() {
            var x = this.name.toLowerCase().charCodeAt(0) <= "g".charCodeAt(0)) ? 1 : 41;
                return Math.floor(Math.random()*40+x);
            })();
    };
发布评论

评论列表(0)

  1. 暂无评论