문제

나는 JavaScript에 대한 더 나은 실무 지식을 얻으려고 노력하고 있습니다. 그래서 저는 Douglas Crockford의 "JavaScript The Good Parts"라는 책을 샀습니다.

나는 현재 프로토 타입을 파악하는 데 어려움을 겪고 있습니다. 아래의 모든 것은 // 프로토 타입 예제를 누르기 전까지 내 브라우저에서 작동하는 것 같습니다. 누군가 내가 왜 출력을 얻을 수 없는지 알아보기 위해 그것을 볼 수 있습니까? (내 페이지가 모든 프로토 타입 코드를 댓글을 달지 않으면 공백을 반환합니다)

도움을 주셔서 감사합니다.

배리

var stooge = { 
    "first-name": "Jerome",
    "last-name": "Howard",
    "nickname": "J", 
    "profession" : 'Actor' 
};

// below is augmenting
var st = stooge;
st.nickname = "curly";
// st.nickname and nick are the same because both are ref's to the same object 
var nick = st.nickname;


document.writeln(stooge['first-name']);  //expect Jerome -- this is "suffix" retrieval 
document.writeln(st.nickname); //expect "curly" -- this is "notation" retrieval
document.writeln(nick); //expect "curly"
document.writeln(stooge.profession); 


//PROTOTYPE EXAMPLE; 
if (typeof Object.create !== 'function')
{
    object.create = function(o) {
            var F = function () {}; 
            F.prototype = o; 
            return new F();
};
var another_stooge = Object.create(stooge);
another_stooge['first-name'] = 'Barry'; 
document.writeln(another_stooge['first-name']);
// the below should be inherited from the prototype therefore "Actor" 
document.writeln(another_stooge.profession);
도움이 되었습니까?

해결책

Object에 할당 된 기능 표현식의 끝에 닫는 브레이스가 없습니다. object.create = function(o) {.

//PROTOTYPE EXAMPLE; 
if (typeof Object.create !== 'function')
{
    Object.create = function(o) {  // <--- "Object" instead of "object"
        var F = function () {}; 
        F.prototype = o; 
        return new F();
    };
}  // <--- Closing brace was missing

다른 팁

당신은 라인의 닫는 버팀대를 놓치고있는 것 같습니다. object.create = function(o) {.... 나는 if 진술과 var F = function () {};, 그러나 그렇지 않습니다 function(o).

JavaScript가 (누락 된) 폐쇄 브레이스 전에 모든 것이 실행될 것이 아니라 (아직) 기능 정의의 일부라고 가정하기 때문에 누락 된 닫기 버팀대는 실제로 출력을 억제합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top