Javascript hasOwnProperty의 속성은 무엇입니까?
if (someVar.hasOwnProperty('someProperty') ) {
// do something();
} else {
// do somethingElse();
}
의 올바른 사용 / 설명은 hasOwnProperty('someProperty')
무엇입니까?
someVar.someProperty
객체 someVar
에 이름이있는 속성이 있는지 확인하는 데 사용할 수없는 이유는 무엇 someProperty
입니까?
이 경우 속성은 무엇입니까?
이 자바 스크립트는 어떤 속성을 확인합니까?
hasOwnProperty
호출하는 객체에 인수 이름이있는 속성이 있는지 여부를 나타내는 부울 값을 반환합니다. 예를 들면 :
var x = {
y: 10
};
console.log(x.hasOwnProperty("y")); //true
console.log(x.hasOwnProperty("z")); //false
그러나 객체의 프로토 타입 체인은 보지 않습니다.
for...in
구문 을 사용하여 객체의 속성을 열거 할 때 사용하는 것이 유용합니다 .
자세한 내용을 보려면 ES5 사양 이 항상 그렇듯이 살펴보기에 좋은 곳입니다.
짧고 정확한 대답은 다음과 같습니다.
자바 스크립트에서 모든 객체에는 객체에 대한 메타 정보가있는 내장 키-값 쌍이 있습니다. for...in
객체에 대해 구성 / 루프를 사용하여 모든 키-값 쌍을 반복 할 때이 메타 정보 키-값 쌍도 반복합니다 (정의 적으로 원하지 않음).
사용 hasOwnPropery(property)
필터 아웃 파라미터 직접적 메타 정보 및 검사를 통해 이러한 불필요한 루프 것은 property
객체의 여부에 주어진 사용자 속성이다. 에 의해 필터 아웃 , 그 말은 hasOwnProperty(property)
, 경우에 보이지 않는 property
메타 정보 일명 객체의 프로토 타입 체인에 존재합니다.
이를 true/false
기반으로 부울을 반환 합니다.
다음은 그 예입니다.
var fruitObject = {"name": "Apple", "shape": "round", "taste": "sweet"};
console.log(fruitObject.hasOwnProperty("name")); //true
console.log(Object.prototype.hasOwnProperty("toString");) //true because in above snapshot you can see, that there is a function toString in meta-information
분명했으면 좋겠어!
그것은 확인합니다 :
객체에 지정된 이름의 속성이 있는지 여부를 나타내는 부울 값을 반환합니다.
hasOwnProperty의 그렇지 않은 경우는 true 메소드가 리턴 객체는 지정된 이름의 특성, 거짓이있는 경우. 이 메서드는 속성이 객체의 프로토 타입 체인에 있는지 확인하지 않습니다. 속성은 개체 자체의 구성원이어야합니다.
예 :
var s = new String("Sample");
document.write(s.hasOwnProperty("split")); //false
document.write(String.prototype.hasOwnProperty("split")); //true
요약:
hasOwnProperty()
모든 객체에서 호출 할 수있는 함수이며 문자열을 입력으로받습니다. true
속성이 객체에 있으면 부울을 반환하고 그렇지 않으면 false를 반환합니다. hasOwnProperty()
위치에 Object.prototype
있으므로 모든 개체에 사용할 수 있습니다.
예:
function Person(name) {
this.name = name;
}
Person.prototype.age = 25;
const willem = new Person('willem');
console.log(willem.name); // property found on object
console.log(willem.age); // property found on prototype
console.log(willem.hasOwnProperty('name')); // name is on the object itself
console.log(willem.hasOwnProperty('age')); // age is not on the object itself
In this example a new Person object is created. Each Person has its own name which gets initialized in the constructor. However, the age is not located on the object but on the prototype of the object. Therefore hasOwnProperty()
does return true
for name and false
for age.
Practical applications:
hasOwnProperty()
can be very useful when looping over an object using a for in
loop. You can check with it if the properties are from the object itself and not the prototype. For example:
function Person(name, city) {
this.name = name;
this.city = city;
}
Person.prototype.age = 25;
const willem = new Person('Willem', 'Groningen');
for (let trait in willem) {
console.log(trait, willem[trait]); // this loop through all properties including the prototype
}
console.log('\n');
for (let trait in willem) {
if (willem.hasOwnProperty(trait)) { // this loops only through 'own' properties of the object
console.log(trait, willem[trait]);
}
}
hasOwnProperty is a normal Javascript function that takes a string argument.
In your case somevar.hasOwnProperty('someProperty') it check the somevar function has somepropery or not ,, it return true and false
Say
function somevar() {
this.someProperty= "Generic";
}
function welcomeMessage()
{
var somevar1= new somevar();
if(somevar1.hasOwnProperty("name"))
{
alert(somevar1.hasOwnProperty("name"));// it will return true
}
}
You use object.hasOwnProperty(p) to determine if object has an enumerable property p-
object can have its own prototype, where 'default' methods and attributes are assigned to every instance of object. hasOwnProperty returns true only for the properties that were specifically set in the constructor, or added to the instance later.
to determine if p is defined at all, anywhere, for the object, use if(p instanceof object), where p evaluates to a property-name string.
For example, by default all objects have a 'toString' method, but it will not show up in hasOwnProperty.
It checks if an object has a property. It works the same as if(obj.prop)
, as far as I know.
참고 URL : https://stackoverflow.com/questions/9396569/javascript-what-is-property-in-hasownproperty
'UFO ET IT' 카테고리의 다른 글
어떻게 Java.util.Map을 현명한 방법으로 소포에 작성합니까? (0) | 2020.11.26 |
---|---|
Xcode 4.3을 설치 한 후 MacPort 기능을 사용할 수 없습니다. (0) | 2020.11.26 |
Xcode-Xcode 6.1에서 대상을 삭제하는 방법은 무엇입니까? (0) | 2020.11.26 |
GitHub 웹 사이트에서 디렉토리 / 폴더의 이름을 바꾸는 방법은 무엇입니까? (0) | 2020.11.26 |
모범 사례 자바 스크립트 및 다국어 (0) | 2020.11.26 |