小编Sar*_*eet的帖子

Javascript ::为什么Object.hasOwnProperty('caller')返回true?

Object继承自Function.prototype,而Function.prototype继承自Object.prototype.

这是因为在内部,Object实际上是一个函数

function Object(){[native code]}
Run Code Online (Sandbox Code Playgroud)

这就是我们可以写代码的原因

var ob=new Object();
Run Code Online (Sandbox Code Playgroud)

Object继承了Function.prototype中的'caller','arity'等属性

然而(这是令人困惑的)

alert(Object.hasOwnProperty('caller')); //returns TRUE ! surprising
Run Code Online (Sandbox Code Playgroud)

不应该返回false,因为Object实际上从Function.prototype继承了'caller'属性?

同样的方式

alert(Function.hasOwnProperty('caller')); 
/*returns True. expected 'false' as Function object has no property of its own and inherits everything from Function.prototype*/

alert(Number.hasOwnProperty('caller')); //again returns true, unexpectedly
Run Code Online (Sandbox Code Playgroud)

那么,有人知道为什么会这样吗?

非常感谢你.我希望我听起来不天真

编辑

尝试Object.getOwnPropertyNames(Object)确实'caller'直接在Object本身作为属性返回.所以Object.hasOwnProperty('caller')内容属实

但是,现在问题是为什么在MDN文档中,'caller'提到继承自Function.所以它肯定会导致混乱.

这是文档中的一些错误吗?谢谢.

编辑-2

我能否得出Object有自己的结论

caller,length等属性为偶数 Object.lengthObject.__proto__.length不一样.如果Object确实从它继承了长度属性,那应该是相等的[[prototype]],Function.prototype 但事实并非如此

问题是,为什么MDN提到对象只是继承caller,length,arity,等从它的[[prototype]]对象?它有点误导恕我直言

javascript inheritance

5
推荐指数
1
解决办法
151
查看次数

如何从plsql中的存储过程获取java代码中的结果集

我正在使用MVC模式我有两个表:Employee和Address

说,员工就像

-------------------
Id  | Name   | DeptId
-------------------
101 | Jake   | 501  
102 | Donald | 502
Run Code Online (Sandbox Code Playgroud)

我有一个像这样的部门表

-----------------------------
DeptId | Name | Description
-----------------------------
501    | IT   | software assistance  
502    | HR   | Human resources
Run Code Online (Sandbox Code Playgroud)

现在,因为我使用MVC,这些表被映射到类

@Table(name="Employee")
Class Employee{
   @Id
   @Column(name="Id")
   private Long id;

   @Column(name="Name")
   private String name;

   @Column(name="DeptId")
   private Long deptId;

   @ManyToOne
   @JoinColumn(name="DeptId", referencedColumnName="id", insertable=false,updatable=false)
   private Department dept;

   //getters and setters go here
}
Run Code Online (Sandbox Code Playgroud)

和其他类部门(映射到部门表)

@Table(name="Department")
    Class Department{
       @Id
       @Column(name="Id")
       private Long id;

       @Column(name="Name")
       private …
Run Code Online (Sandbox Code Playgroud)

java oracle plsql stored-procedures jpa

5
推荐指数
1
解决办法
758
查看次数

为什么这个递归构造函数不会导致编译时错误?

考虑以下代码

class ConstructorDemo2{
    ConstructorDemo2(){
        this(1);
    }
    ConstructorDemo2(int i){
        this();
    }
    public static void main(String[] args){
        new ConstructorDemo2();
    }
}
Run Code Online (Sandbox Code Playgroud)

Kathy Sierra的SCJP6书在第144页上说,这样的代码可能无法检测到并导致StackOverflowError.但与此同时,我们知道子类构造函数ALWAYS必须使用super()[默认由编译器提供] 来调用超类构造函数,但在下面的代码示例中,两个构造函数都在调用this()(相互调用).
它确实显示了error: recursive constructor invocation我的OpenJDK编译器中的错误,但Kathy Sierra的书中说这样的代码可能不会被编译器检测到并且会在运行时抛出异常.

因此,如果在SCJP/OCJP考试中向我展示了这样的代码示例,并询问它是否会编译,那么答案是什么?看起来有点暧昧

java recursion constructor scjp

3
推荐指数
1
解决办法
449
查看次数

Collections类中checkedList方法的用途

api说"返回指定集合的​​动态类型安全视图" 但是当泛型可以在编译时检测到是否有任何可疑内容被插入到集合中时,它仍然需要它.

该文档说"语言中的泛型机制提供了编译时(静态)类型检查,但是可以通过未经检查的强制转换来破坏这种机制 "

但即使这样也行不通

List<Integer> list = new ArrayList<Integer>();
Object o = new Float(1.2);
Integer i = (Integer)o; // line3
list.add(i);
Run Code Online (Sandbox Code Playgroud)

但是即使这一个wud在运行时ClassCastException因为被抛出line3而失败,因为o实际上持有a Float,无法将其转换为Integer.所以,我想知道如何围绕静态编译时泛型检查来证明checkList方法的存在

java generics

2
推荐指数
1
解决办法
105
查看次数

列表中的contains()方法无法按预期工作

contains()方法的api 说

“如果此列表包含指定的元素,则返回true。更正式地说,当且仅当此列表包含至少一个元素e使得(o == null?e == null:o.equals(e))时,返回true。

我覆盖了equals()我班上的方法,contains()但当我检查时仍返回false

我的代码

class Animal implements Comparable<Animal>{
    int legs;
    Animal(int legs){this.legs=legs;}
    public int compareTo(Animal otherAnimal){
        return this.legs-otherAnimal.legs;
    }
    public String toString(){return this.getClass().getName();}

    public boolean equals(Animal otherAnimal){
        return (this.legs==otherAnimal.legs) && 
                (this.getClass().getName().equals(otherAnimal.getClass().getName()));
    }

    public int hashCode(){
        byte[] byteVal = this.getClass().getName().getBytes();
        int sum=0;
        for(int i=0, n=byteVal.length; i<n ; i++)
            sum+=byteVal[i];
        sum+=this.legs;
        return sum;
    }

}
class Spider extends Animal{
    Spider(int legs){super(legs);}
}
class Dog extends Animal{
    Dog(int legs){super(legs);}
}
class Man extends …
Run Code Online (Sandbox Code Playgroud)

java collections java-collections-api

2
推荐指数
1
解决办法
1186
查看次数